Developer forum

Forum » Development » Loading ProductCollection issues and bugs

Loading ProductCollection issues and bugs

Remi Muller
Reply

I am having some issues trying to load a ProductCollection via the ecommerce api.
See below for the current 3 issues i am having.

 

DW 8.2.3.3

 

Dynamicweb.eCommerce.Products.Product.GetProductsByGroupID
1: sorting issue, GroupProductRelationSorting is not in the field list. This breaks the function to able to sort on UserDefined.
2: Group language issue, if a group is translated in multiple languages this results in duplicate products because of these relations. The sql query should also filter on the group languageid.

 

Dynamicweb.eCommerce.Products.Group.GetGroupByID
3: caching issue, products are cached for no apparent reason. For example when changing the productname this is not visible in this collection.
It looks like this cache is never refreshed until the application pool recycles.
There is a work around by setting Group.Products=null before iterating the Group.Products collection then it gets refreshed

 

Are there other or better way to load a ProductCollection based on a groupid?
Can a dev confirm these issues and get them fixed where needed?

 


Replies

 
Nicolai Høeg Pedersen
Reply

I'll have a dev take a look at this.

 

Do you by any chance have some sample code?

 

Thanks, Nicolai

 
Remi Muller
Reply
//var productcollection = Dynamicweb.eCommerce.Products.Product.GetProductsByGroupID(groupid);
			Dynamicweb.eCommerce.Products.ProductCollection productcollection = null;
			var group = Dynamicweb.eCommerce.Products.Group.GetGroupByID(groupid);
			if (group != null) {
				//workaround: make sure group object reloads all products by setting it to null, otherwise they might be comming from cache
				group.Products = null;
				//by calling group.Products the list gets reloaded
				productcollection = group.Products; 
			}
			
			if (productcollection == nullreturn new List<ProductListItem>();
			
			return productcollection.OrderBy(a => a.RelationSorting).Select(a =>
				new ProductListItem
				{
					ID = a.ID,
					Titel = a.Name,
					Afbeelding = a.ImageSmall
				}
			).ToList<ProductListItem>();
 
Remi Muller
Reply

code above demonstrates issue 3

 
Remi Muller
Reply
//if a product is attached to multiple translated groups it gets duplicated or more in this collection depending on the group translation
			var productcollection = Dynamicweb.eCommerce.Products.Product.GetProductsByGroupID(groupid);
			
			if (productcollection == nullreturn new List<ProductListItem>();
 
			//sorting on RelationSorting does not work because it is not in the select criteria of Dynamicweb.eCommerce.Products.Product.GetProductsByGroupID
			return productcollection.OrderBy(a => a.RelationSorting).Select(a =>
				new ProductListItem
				{
					ID = a.ID,
					Titel = a.Name,
					Afbeelding = a.ImageSmall
				}
			).ToList<ProductListItem>();
 
Remi Muller
Reply

code above demonstrates issues 1 and 2

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Remi,

 

Let me try and explain why you're experiencing these issues.

 

1) The value for the property RelationSorting is not part of the query because that value is potentially different for each group the product is part of. You might argue that the product is only part of one group in this context but in order to keep things consistant, this value is not loaded when a ProductCollection is filled with products. It's loaded on-demand when ProductCollection.Sort() is invoked with the UserDefined parameter. See method SortProductsByCustomSorting in the sample code at the bottom of this post.

 

2) I am unable to reproduce this issue. When invoking the GetProductsByGroupID with only the group id parameter, I get all products in the current language context. It is possible to specify a different language by using the overload shown in the example below (GetProductsByGroupIDAndLanguage).

 

3) It is true that the Group class uses internal caching but it's not for no reason. Groups are queried a lot and are changed relatively little. Therefore, it makes sense to cache them. Sometimes, however, it is required to invalidate the cache. This happens automatically when saving a group or a product through the API.

So what you are experiencing should only occur when you manipulate the database directly and circumvent the API. In this case, you should invoke Group.ClearCache() or Group.Save() or Product.Save() as all of these actions will clear the cache. Your own workaround will also work. I have included code in the sample (GetProductsFromGroup) that illustrates this.

 

Sample code:

    public class eComApiTest
    {
        public void SortProductsByCustomSorting(ProductCollection productsToSort, ProductCollection.SortDirection direction)
        {
            // Invoke sort on the ProductCollection
            productsToSort.Sort(ProductCollection.SortBy.UserDefined, direction);
        }

        public ProductCollection GetProductsByGroupIDAndLanguage(string groupID, string languageID)
        {
            // Get products by <groupId> and <languageId>
            var productsFromGroupInGivenLanguage = Product.GetProductsByGroupID(groupID, true, languageID);

            // Return products
            return productsFromGroupInGivenLanguage;
        }

        public ProductCollection GetProductsFromGroup(string groupId, string languageId)
        {
            // Get a product known to be in <groupId>
            var product = Product.GetProductByID("MyID", null, languageId);
            // Give it a new name
            product.Name = "New name";
            // Save it - will also clear Group cache
            product.Save();

            // Get <groupId>
            var group = Group.GetGroupByID(groupId, languageId);
            // Get products - will contain <product> and <product> will be named "New name"
            var products = group.Products;

            //Return products
            return products;
        }
    }

 

I hope this clears the air a bit :)

 

- Jeppe

 
Remi Muller
Reply
Oke thanks. This helps. I have a few questions though and item 2 is definitely a bug. Please read the following:
 
 
1)
var productcollection = Dynamicweb.eCommerce.Products.Product.GetProductsByGroupID(groupid);
productcollection.Sort(Dynamicweb.eCommerce.Products.ProductCollection.SortBy.UserDefined, Dynamicweb.eCommerce.Products.ProductCollection.SortDirection.Ascending);
 
The userdefined is not loaded on demand in this case. Should it be??
 
2)
Here we have a bug i think.
I profiled the query and there is an error when filtering on a languageid.
This is the query run:
SELECT EcomProducts.* FROM (EcomGroupProductRelation INNER JOIN EcomProducts ON EcomGroupProductRelation.GroupProductRelationProductID=EcomProducts.ProductID) INNER JOIN EcomGroups ON EcomGroupProductRelation.GroupProductRelationGroupID=EcomGroups.GroupID WHERE EcomGroupProductRelation.GroupProductRelationGroupID  = 'GROUP14'  AND ((EcomProducts.ProductLanguageID)='LANG1') 
 
and it should be:
SELECT EcomProducts.* 
FROM (EcomGroupProductRelation 
INNER JOIN EcomProducts ON EcomGroupProductRelation.GroupProductRelationProductID=EcomProducts.ProductID) 
INNER JOIN EcomGroups ON EcomGroupProductRelation.GroupProductRelationGroupID=EcomGroups.GroupID 
WHERE 
EcomGroupProductRelation.GroupProductRelationGroupID  = 'GROUP14'  
AND ((EcomProducts.ProductLanguageID)='LANG1') 
and dbo.EcomGroups.GroupLanguageID = 'LANG1'
 
I added the bold part criteria. The query is missing the language filter on EcomGroups which can duplicate the products if there are translated groups.
 
3)
Ok clear but keep in mind i changed the product name through the ecom backend edit product. Not directly in the DB.
But in this case it is a backend proces which does run in the same application pool actually but is separated in a new application.
This is the reason that the cache is not refreshed?
 
 
Remi Muller
Reply

hi Jeppe,

I hope you have some time to answer my questions above and confirm the bug? Thanks.

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Remi,

 

I finally figured out, why you were getting different results than me.

 

1) and 2) which were indeed bugs on your version has been fixed on the version I was testing on. The fix will be out with 8.3.1.0.

 

3) The cache is not shared between the two applications, therefore, you cleared it on one but not on the other and this is the reason.

 

Sorry for the confusion.

 

- Jeppe

Votes for this answer: 1

 

You must be logged in to post in the forum