Developer forum

Forum » Ecommerce - Standard features » Product price based on total amount of products in cart

Product price based on total amount of products in cart

Claus Kølbæk
Claus Kølbæk
Reply

Hey

We’re using a v4 integration to Business Central’s OData with the “DynamicwebSalesPrices” blueprint, and it’s working as expected. However - the customer has a price condition where, if you buy at least 10 items in total (any products), the price of a specific product should also be lower. So it’s not just a quantity break on that specific product—otherwise the “When” option would cover it.

As I understand it, this isn’t supported out of the box. Does anyone have an idea for the smartest way to handle this? I’m considering doing it through subscribers, but maybe there’s a better solution I haven’t thought of 😊


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

I would say this is a classic discount. If regular price is 15, you can create a discount that triggers onm 10 productrs and give e.g. 5 dkk or 33% in discount.

You can also create priceprovider that only reacts on said product, and in that provider, see if there is a cart, and if there is return a price from e.g. a custom field. Be very careful with null checks int hat one.

You might also find some inspiration below in a custom discount using a DiscountExtenderBase and the IDiscountExtenderCalculateProductDiscount

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

namespace Dynamicweb.Ecommerce.Orders.Discounts
{
    public class SetAmountFromProductAsFinalPriceExtender : DiscountExtenderBase, IDiscountExtenderCalculateProductDiscount
    {
        [AddInParameter("Set price with discount to 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)
        {
            var context = new PriceContext(currency, country, Services.Shops.GetShop(Dynamicweb.Frontend.PageView.Current()?.Area?.EcomShopId), UserContext.Current.User, Common.Context.ReverseChargeForVatEnabled, Common.Context.OrderTime);
            return GetDiscount(context, product);
        }

        private PriceInfo GetDiscount(PriceContext context, Product product)
        {
            //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 > 0d)
            {
                //Load the price information located on the product in a custom field into a price calculated to handle vat etc.
                var priceFromCustomField = PriceCalculated.Create(context, new PriceRaw(amountFromProductField, context.Currency));
                priceFromCustomField.Calculate();

                //Find the calculated price for this product
                var priceFromProduct = product.GetPrice(context);

                if (CalculateDiscountToReachSpecifiedDiscountPrice)
                {
                    //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(context, new PriceRaw());
                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);
            return fieldValue > 0.0d;
        }

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

 

You must be logged in to post in the forum