How to override the price at the time of ADD to cart

Umbraco commerce version 13.1.17

I am working on a product configurator where the user adds the parts and accessories and build a new product. This product is not in Umbraco. I have added a dummy product to umbraco with Inventory 1 and price 1. I want to override the price with a calculated price value at the time of ADDTOCART.
I created a customOrderLineCalculator and set price with calculated price. But the override logic is not hit on ADDTOCART.

How to override the price of the product upon add item to cart?

CartController.cs
var order = _commerceApi.GetOrCreateCurrentOrder(store.Id)
   .AsWritable(uow)
   .AddProduct(postModel.productKey, "", postModel.Quantity, new Dictionary<string, string>{
        { "overrideprice", totalprice3D }
   });
_commerceApi.SaveOrder(order);
uow.Complete();
 
startup.cs
.AddUmbracoCommerce(options =>
  {
      //options.Services.AddUnique<IProductCalculator, CustomCalculator>();
      options.Services.AddUnique<IOrderLineCalculator, CustomOrderLineCalculator>();
  })
CustomOrderLineCalculator.cs
public class CustomOrderLineCalculator : IOrderLineCalculator
{
    private readonly IOrderLineCalculator _defaultCalculator;

    public CustomOrderLineCalculator(IOrderLineCalculator defaultCalculator)
    {
        _defaultCalculator = defaultCalculator ?? throw new ArgumentNullException(nameof(defaultCalculator));
    }

    public TaxRate CalculateOrderLineTaxRate(OrderReadOnly order, OrderLineReadOnly orderLine, TaxSource taxSource, TaxRate fallbackTaxRate)
    {
        Console.WriteLine("CustomOrderLineCalculator CalculateOrderLinePrice called 2");
        throw new NotImplementedException();
    }

    public Price CalculateOrderLineUnitPrice(OrderReadOnly order, OrderLineReadOnly orderLine, Guid currencyId, TaxRate taxRate)
    {
        Console.WriteLine("CustomOrderLineCalculator CalculateOrderLinePrice called 1");
        if (orderLine != null && orderLine.Properties != null &&
            orderLine.Properties.TryGetValue("overrideprice", out var overridePriceStr))
        {
            if (!string.IsNullOrEmpty(overridePriceStr))
            {
                if (decimal.TryParse(overridePriceStr, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal overridePrice))
                {
                    // Return a new Price using the override value.
                    return new Price(overridePrice, taxRate, currencyId);
                }
            }
        }
        return _defaultCalculator.CalculateOrderLineUnitPrice(order, orderLine, currencyId, taxRate);

    }
}

In your startup.cs file, when are you calling AddUmbracoCommerce? This must occur before calling AddComposers as Umbraco Commerce has a default composer that runs and sets up a standard configuration if a user defined configuration hasn’t yet been configured.

Thanks. It worked

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.