Developer forum

Forum » Ecommerce - Standard features » Buy X and Y get 50% of Z discount type

Buy X and Y get 50% of Z discount type

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I know there have been a few discussions in the past around combined discounts but I want to refresh my memory about the availability of this scenario.

I am sure it is not supported out of the box, I am just wondering if this can be accomplished with a custom discount and what would be the best approach in DW 9.12.x

Thank you,

Adrian 


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

It is not supported no.

 

You need to create a DiscountExtenderBase and add either IDiscountExtenderCalculateProductDiscount or IDiscountExtenderCalculateOrderDiscount

Since this is something that relates to an order and not the product it self, you should probably do an order discount and use IDiscountExtenderCalculateOrderDiscount

Here an example using product discount - but the idea is the same

public class SetAmountFromProductAsFinalPriceExtender : DiscountExtenderBase, IDiscountExtenderCalculateProductDiscount
    {
        [AddInParameter("Calculate discount based on amount field"), AddInDescription("Will set the final discounted price of a product to the value specified on the product custom field."), AddInParameterEditor(typeof(YesNoParameterEditor), "")]
        public bool CalculateDiscountToReachSpecifiedDiscountPrice { get; set; }

        public PriceInfo GetDiscount(Product product, Currency currency, Country country)
        {
            //Locating the amount set on the product in the custom field. We want to use this value as the final price after discount for this product.
            double amountFromProductField = Converter.ToDouble(product.ProductFieldValues.GetProductFieldValue(Discount.AmountProductFieldName).Value);
            if (amountFromProductField > 0)
            {
                //Load the price information located on the product in a custom field into a price calculated to handle vat etc.
                var priceFromCustomField = PriceCalculated.Create(currency, country, new PriceRaw(amountFromProductField, currency));
                priceFromCustomField.Calculate();

                if (CalculateDiscountToReachSpecifiedDiscountPrice)
                {
                    //Find the calculated price for this product
                    var priceFromProduct = product.GetPrice(currency.Code, country.Code2);

                    //calculate what the discount must be in order to achieve a product price with discount that reflects what is in the custom field on the product
                    var theDiscount = priceFromProduct.Substract(priceFromCustomField);
                    return theDiscount;
                }
                else
                {
                    return priceFromCustomField;
                }
            }
            else
            {
                var calculated = PriceCalculated.Create(currency, country, new PriceRaw(0d, currency));
                calculated.Calculate();
                return calculated;
            }
        }

        public override bool DiscountValidForProduct(Product product)
        {
            //This discount needs a field name specified in order to work
            if (string.IsNullOrEmpty(Discount.AmountProductFieldName)) { return false; }

            //This discount only works if the field has a positive value
            double fieldValue = Converter.ToDouble(product.ProductFieldValues.GetProductFieldValue(Discount.AmountProductFieldName).Value);
            if (fieldValue > 0) { return true; }
            return false;
        }

        public override bool DiscountValidForOrder(Order order)
        {
            //this discount type only applies for products
            return false;
        }
    }
}
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Thank you very much.

It is a very good start.

Adrian

 

You must be logged in to post in the forum