Developer forum

Forum » Development » ProductListeTemplateExtender Remove product from ProductList

ProductListeTemplateExtender Remove product from ProductList

Dmitrij Jazel
Reply

Hi guys,

I have some rather simple ProductListTemplateExtender

using Dynamicweb;
using Dynamicweb.eCommerce.Products;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DWCustomCodes
{
    public class ProductRemover : ProductListTemplateExtender
    {
        public override void ExtendTemplate(Dynamicweb.Rendering.Template template)
        {            
            foreach (Product p in this.ProductList)
            {
                string suffix = p.ID.Substring(0, 3);
                
                if (suffix == "AU_")
                {
                    p.RemoveItem(p.ID);
                }
            }
            this.ProductList.SaveAllProducts();
        }        
    }
}

Issue, p.RemoveItem(p.ID); is not removing product.

Question: is there any method I could try calling, or removing item in another way? 

I also tryed to add this.ProductList.SaveAllProducts(); after loop is done, but that didn't help.

During the loop this.ProductList.Remove(p); this cannot Remove product while in the same loop.

Tryed to collect all product I would need to remove, and later loop through that collection, and removing that product from this.ProductList, but no results :-/ I am sure code gets invoked, am quite surprised that something is not OK in here.

 

Any suggestions, examples, oppinions are more than welcome.

/Dmitrij

 

 


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Dmitrij,

The reason this fails is because the ProductListTemplateExtenders are invoked after the products have already been rendered. If you need to remove products from the result set, you should subscribe to one of the following notifications instead:

  • Dynamicweb.Notifications.eCommerce.ProductList.BeforeSort
  • Dynamicweb.Notifications.eCommerce.ProductList.BeforePaging
  • Dynamicweb.Notifications.eCommerce.ProductList.BeforeRender

Depending on which of these you choose, you may run into some issues with paging -- especially if you're using Optimized Product Retrieval that only fetches the exact list of products for the given page. If this is the case, you may want to consider implementing a custom ProductListCollectionProvider or not use Optimized Product Retrieval.

- Jeppe

 
Dmitrij Jazel
Reply

Hej Jeppe,

Well, we do use recomended settings for large stock. Product catalog paragraph module settings (http://screencast.com/t/zSdn5x2fy6YM)

And Product Index is build ofcourse.

 

Please correct me if I am wrong, but by saying "Optimized Product Retrieval" you mean all this settings? or there is something I missed :-)

 

And I think that I did run into that issue you mentioned with Paging. Now trying to find ProductListCollectionProvider, but can't seem to find it. :-/ http://screencast.com/t/yhNMrecxDbI

 

/Dmitrij

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

When I say Optimized Product Retrieval, I mean the checkbox called "Enable optimized product retrieval", but as you correctly state, it's not recommended to turn this off. Though, it is necessary in some specific cases. If you use the index, this has no effect at all because it's not used with the index.

The same goes for the ProductListCollectionProvider. You cannot use this and index at the same time. Unfortunately, it's not possible to avoid the paging issue using any of the aforementioned methods. If you need to limit the result set from the index, you need to do it before the query is sent to the index. Otherwise the paging will be incorrect whenever you remove products from the list.

To manipulate the query, you need to subscribe to the Dynamicweb.Notifications.eCommerce.SearchFilters.BeforeQueryingIndex notification, which takes a Dynamicweb.Notifications.eCommerce.SearchFilters.IndexQueryNotificationArgs. Them you manipulate the Query property of the args object with any limitation you need. This should ensure that the result only contains what you need, and therefore, the paging should also be correct.

 

You must be logged in to post in the forum