Developer forum

Forum » Development » Product Discount in ProductIndexBuilder/ExtendDocument

Product Discount in ProductIndexBuilder/ExtendDocument

Casper Hermann Rasmussen
Casper Hermann Rasmussen
Reply

Hi,

I need to know if a product has a discount or not in a ProductIndexBuilder/ExtendDocument().
The purpose is to enrich other fields with values ​​depending on whether a product has a discount or not.

  • I would like to avoid Services.Discounts.GetDiscounts() as there are many rules that must be followed for a discount.
  • I have tried listening on the IndexDocument parameters, but there are all price values ​​0. The IndexDocument does not return the before price and the discount price - only a price.
  • I've tried Services.Products.GetProductById(), GetDiscountMatrix() and GetPrice() but they require a Dynamicweb.Context.Current context.

I would prefer the last approach (Services.Products), as I see advantages in being able to take data directly from the product service.
Is it possible to declare a Dynamicweb.Context.Current context?
Is there another approach?

Regards,
Casper


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Casper

There is no simple approach to this.

Discounts are related to a lot of things - like time, currencies, orders etc. etc. So if a product has a discount or not is very difficult to not do very buggy. That said, it is possible to get the most brutal part of a product discount into the index. But I have no specific handles for you.

I know Nuno have some code from old times doing part of this. You might find it here in the forum.

Or Nuno?

BR Nicolai

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Casper,

 

Can you expand on your final goal? Maybe we can find another solution for it.

 

As Nicolai described, what you need "can" be done, but shouldn't. It would force discounts to be only configured in a certain way, otherwise it becomes a very time-consuming and bug-prone. The problem becomes that the customer may forget the boundaries, or people change in the organization and documentation is no-help, so it becomes a bunny-trail of issues.

 

Best Regards,

Nuno aguiar

 
Casper Hermann Rasmussen
Casper Hermann Rasmussen
Reply

Hi both,

I understand the complexity of a product discount in an index (cached documents vs. instant product model)

What is the recommended method, if you want to filter/faceting on products that have a product discount?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

So just if a product is member of a discount? Because you cannot see if the discount is actually valid.

You can create a discount extender that can find something out - here is a rough example - it will probably have performance problems and you need to fiddle what discounts you want to trigger true or false on.

namespace Dynamicweb.Ecommerce.Examples
{
    public sealed class IndexBuilderExtenderExample : IndexBuilderExtenderBase<ProductIndexBuilder>
    {
        public override void ExtendDocument(IndexDocument document)
        {
            if (document.ContainsKey("ID"))
            {
                string productid = (string)document["ID"];
                string variantid = (string)document["VariantID"];
                if (!string.IsNullOrEmpty(productid))
                {

                    bool productHasDicount = false;
                    double productDicountPercent = 0;

                    var product = Dynamicweb.Ecommerce.Services.Products.GetProductById(productid, variantid, useDefaultLanguage: true);
                    var priceContext = new PriceContext(Services.Currencies.GetDefaultCurrency(), Services.Countries.GetCountries().FirstOrDefault(), Services.Shops.GetDefaultShop(), null, false, DateTime.Now());

                    foreach (var discountInfo in product.GetDiscountMatrix(priceContext, string.Empty, 0))
                    {
                        if (discountInfo.Discount.Active && discountInfo.Discount.ProductQuantity <= 1)
                        {
                            //will find the first available discountInfo and use that percentage.
                            productHasDicount = true;
                            productDicountPercent = discountInfo.Discount.Percentage;
                            break;
                        }
                    }
                    if (productHasDicount)
                    {
                        document.Add("Custom_ProductHasDicount", "True");
                        document.Add("Custom_ProductDicountPercent", productDicountPercent.ToString("P2"));
                    }
                    else
                    {
                        document.Add("productHasDicount", "False");
                    }
                }
            }           
        }
    }
}

 

You must be logged in to post in the forum