Developer forum

Forum » Development » Modifying the ProductList from a ProductListTemplateExtender

Modifying the ProductList from a ProductListTemplateExtender


Reply

Hi there,
 

I am trying to modify the ProductList property of a ProductListTemplateExtender by adding or removing products using some custom logic. Here's a quick example:

public override void ExtendTemplate(Dynamicweb.Templatev2.Template template)
{
string sql = "SELECT * FROM Ecomproducts WHERE ProductNumber = 'Test1234'";
ProductCollection productsToAdd = Product.getProductBySQL(sql);
foreach (Product item in productsToAdd)
{
ProductList.Add(item);
}
}


Although the code works fine and at the end of the ExtendTemplate method my ProductList property contains the new products, the changes are not reflected at the front-end. The same is true when I remove items, or when I call Clear on the ProductList.
 

Is Dynamicweb passing a copy of the internal list to the extender, and should I consider the ProductList to be read-only? If so, what other ways are there to change the products being displayed?


I also tried manually rendering the list using something like this:

new Renderer().RenderProducts(ProductList, template);


While this indeed outputs my own items, I also get the initial items, and the product- and page counts are off.

Any suggestions?

 

Thanks in advance.

Imar


Replies

 
Reply
Just changed my logic a bit and started using a NotificationSubscriber instead of a TemplateExtender. Using Dynamicweb.Notifications.eCommerce.ProductList.BeforeSort as the subscriber seems to work fine.

Is BeforeSort the way to do it? I also tried BeforeRender but there the collection appears to be read-only as well.

Any advice would be very welcome.

Imar
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
Hi Imar

BeforeSort is the best way if you want your products to be sorted along with the rest of the product list. This will also remove potential duplicates of products. If, however, you want your products to just appear at the end if the product list and potential duplicates to be outputted as well, the use BeforePaging instead.

- Jeppe

 

You must be logged in to post in the forum