Table of Contents

Interface IDiscountExtenderCalculateOrderDiscount

Namespace
Dynamicweb.Ecommerce.Orders.Discounts
Assembly
Dynamicweb.Ecommerce.dll

Implement this interface on a DiscountExtenderBase implementation to take over the calculation of a discount for an Order

public interface IDiscountExtenderCalculateOrderDiscount

Examples

using Dynamicweb.Core;
using Dynamicweb.Ecommerce.International;
using Dynamicweb.Ecommerce.Prices;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;

namespace Dynamicweb.Ecommerce.Orders.Discounts
{
    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 priceContext = new PriceContext(currency, country);
                    var priceFromProduct = product.GetPrice(priceContext);

                    //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;
        }
    }
}

Remarks

If this interface is implemented it takes over discount calculation for an order.

Methods

GetDiscount(Order, PriceInfo)

Return the discount amount as a priceinfo that should be given to the passed order.

PriceInfo GetDiscount(Order order, PriceInfo calculatedDiscount)

Parameters

order Order

The order to calculate the discount for

calculatedDiscount PriceInfo

The discount calculated by this discount instance

Returns

PriceInfo

The amount that is wanted to be given in discount

Remarks

Override or adjust the calculation made by the discount implementing this method.

To top