Developer forum

Forum » Feature requests » Missing discount product loop

Missing discount product loop

Jakob Westhausen
Reply

When creating sales discounts with multiple products (http://prntscr.com/6z4d9c) it is only possible to output basic information about the discount, like name, description, type and so forth (http://prntscr.com/6z4jb7). But I need to display the products as well like a bundle of configurable products/variants (http://scanpan2.dev.nozebrahosting.dk/Files/Templates/Designs/Scanpan/assets/markup/product-discount.html). It would be great if the "ProductDiscounts" loop also provided a product loop so the customer easily could create product packages which triggers a discount.

Regards
Jakob Westhausen


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Jakob

It is not that simple that we can just do that - discounts are providers, and the list of products is coming from the provider. Lots of technical explanations. But you can easily do it your self in a Razor template or by making a ProductTemplateExtender that finds bundle products.

This example shows how to find the data for a specific discount and render them in a product template extender. Code also attached.

 

using Dynamicweb.eCommerce.Products;
 
namespace Dynamicweb.Examples.CSharp.eCommerce
{
 public class ProductTemplateExtenderSample : ProductTemplateExtender
 {
  public override void ExtendTemplate(Dynamicweb.Rendering.Template template)
  {
   //Only render bunde products when in product view mode - not on lists.
   if (!this.IsDetailView)
   {
    return;
   }
   //Loop all salesdiscounts - they are cached.
   foreach (var discount in Dynamicweb.eCommerce.Orders.SalesDiscounts.SalesDiscount.getSalesDiscounts(Dynamicweb.eCommerce.Common.Context.Language))
   {
    //If it is salesdiscounts of the ProductDiscount type, finde the products that is valid for
    if (discount.GetSalesDiscountProvider().GetType() == typeof(Dynamicweb.eCommerce.Orders.SalesDiscounts.ProductDiscount))
    {
     //Get the indstance of the product discount provider
     var productDiscount = (Dynamicweb.eCommerce.Orders.SalesDiscounts.ProductDiscount)discount.GetSalesDiscountProvider();
     //Inside it holds a list of all product and group IDs that are chosen on this instance. Load them into a handler to get a collection of products/groups
     var productHandler = new Dynamicweb.Extensibility.ProductsAndGroupsHandler(productDiscount.ProductsAndGroups, Dynamicweb.Extensibility.ProductsAndGroupsHandler.Types.All);
 
     //Loop all products in this product discount instance.
     bool thisProductIsInBundle = false;
     foreach (Dynamicweb.eCommerce.Products.Product product in productHandler.ProductsSelected)
     {
      //The product being rendered is in this discount
      if (product.ID == this.Product.ID)
      {
       thisProductIsInBundle = true;
       break;
      }
     }
     //Lets render a loop of products
     if (thisProductIsInBundle)
     {
      if (template.LoopExists("ProductBundles"))
      {
       var bundleLoop = template.GetLoop("ProductBundles");
       var productRenderer = new Dynamicweb.eCommerce.Frontend.Renderer();
       foreach (Dynamicweb.eCommerce.Products.Product product in productHandler.ProductsSelected)
       {
        productRenderer.RenderProduct(product, false, bundleLoop);
        bundleLoop.CommitLoop();
       }
      }
     }
 
    }
   }
 
   // Set my custom template tag
  }
 }
}

​Hope this will help you on the way.