Adjusting unit price of cart items

Hi

v13.2.3

I need to change the unit price of one or more products in the cart. I understand it must be done via PriceAdjuster however I’m having issues.

The code in the documentation generates an exception.
Price/Amount Adjustments | Umbraco Commerce

ArgumentNullException: Value cannot be null. (Parameter 'price2')
Umbraco.Commerce.Extensions.AssertionExtensions.MustNotBeNull<T>(T param, string paramName)

Also, the example is adding the adjust to the subtotal, but I need to adjust the specific unit prices of the products because it affects the line total which affects the tax (special rules).

Thanks.

Attempting add to the OrderLines property results in the same error:

args.OrderLines[line.Id].UnitPriceAdjustments.Add(priceAdjustment);

@mattbrailsford any idea?

Can you share what code you’ve got so far?

Thanks @mattbrailsford

So minor progress, btw the args param is confusing as it contains lots properties, most of which are useless and/or null. I was using the UnitPrice but I see Base is obsolete and i’ve changed to BasePrice. However this code is ADDING the new price to the current price which isn’t correct, it should be changing the current price to the new price.

In this test i’m just altering the price to 0.1 from whatever it is however I see on the front end its actually adding the .01 to the current price.

public class CartValidationProductPriceAdjuster : PriceAdjusterBase
{
    public override void ApplyPriceAdjustments(PriceAdjusterArgs args)
    {
        foreach (var line in args.Order.OrderLines)
        {
            // reduce to 0.1 for testing 
            var newPrice = new Price(.01M,  line.BasePrice.WithoutAdjustments.Tax,
                line.BasePrice.WithoutAdjustments.CurrencyId);

            var priceAdjustment = new ProductPriceAdjustment(
                "Change Unit Price Test", 
                line.ProductReference,
                line.BasePrice.WithoutAdjustments,
                newPrice);
            
             // finally this seems to work (ish) but its adding the adjustment rather than changing the price
             args.OrderLines[line.Id].UnitPriceAdjustments.Add(priceAdjustment);
        }
    }
}

Not sure if this is correct but it seems the ‘newprice’ isn’t actually the new price, its the difference. So if I subtract 0.01 from the current price and negate it (e.g. -(currentpage-0.01)) and use that as the new price, then the price on the front is correct.

Rather confusing tbh, docs could do with some clarification.

So the clue is in the name “Price Adjuster”. It makes an “adjustment” to the price. So you aren’t replacing the price, you are providing some kind of a change to it. Discounts apply a negative adjustment, and fees apply a positive adjustment. So really your adjustment needs to calculate the difference you want to alter the base price by and return that as the adjustment.

An alternative approach might be to use the OrderLineCalculator instead (Calculators | Umbraco Commerce) the orderline calculator is responsible for providing the unit price of an orderline so this is likely closer to what you ware wanting to achieve by completely replacing the unit price with a new value.

Ultimately, the point of “Adjustments” is to provide justifications as to why the price is what it is. Ie, you have a base price and adjustments and the unit price is a clear sum of base price + adjustments. The OrderLineCalculator on the other hand just calculates and provides a unit price which to the outside world doesn’t have a clear reason why it is what it is as there is no record within the order of the price being overridden.

PS If you implement the OrderLineCalculator, you may also need to implement a ProductCalculator. The OrderLineCalculator is responsible for calculating the unit price of an orderline where as the ProductCalculator is used to calculate the price of a product prior to being added to the cart (ie, when displaying prices on the product pages). Depending on what you want to show a different price determines which of these you need to implement.

I hope this brings you some clarity.

1 Like

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