Unit value adjustments seems to set orderline to 0.00

I’ve been racking my brain as to why this isn’t working, I’m hoping its some simple math I have stuffed up along the way (or the implementation is wrong).

What I have essentially is organisation level pricing per product, each product has a base price (aka list price) and a discounted price (the sale price). This was working fine inside of a custom orderline calculator but I noticed the price adjustments weren’t visible on the baskets inside of commerce, nor were they persisted to the database.

What I have done is implemented a PriceAdjuster and attempted to re-use my services to get the values I need (these values are accurate). However it works fine when the quantity of the orderline is 1, but when you start to increase it gets a bit weird. When you set the quantity to 10 the price of the orderline essentially becomes zero.

The price of my products are:

List price: £60.00
Discount: £6.00 (10% converted in the pricing service)
Expected net: £54.00
Expected tax: £10.80
Expected gross: £64.80

Here is how I am attempting to apply unit level pricing:

public class DiscountPriceAdjustment(IServiceProvider serviceProvider) : PriceAdjusterBase
{
    public override  void ApplyPriceAdjustments(PriceAdjusterArgs args)
    {
        bool isAWholesaleProduct = args.Order.OrderLines
            .Any(x => x.Properties.TryGetValue("classification", out var classification)
                      && classification.Value == ProductClassification.Wholesale.ToString());

        if(!isAWholesaleProduct)
        {
            // this discount only applies to wholesale products
            return;
        }

        using var scope = serviceProvider.CreateScope();
        // Retrieve all registered IPricingStrategy implementations
        var pricingStrategies = serviceProvider.GetServices<IPricingStrategy>();

        // Filter to get the Wholesale pricing strategy
        var wholesaleStrategy = pricingStrategies
            .OfType<WholesalePricingStrategy>()
            .FirstOrDefault();

        if (wholesaleStrategy == null)
        {
            throw new InvalidOperationException("Wholesale pricing strategy not found.");
        }

        foreach ((Guid orderKey, OrderLineCalculation? calculation) in args.Calculation.OrderLines)
        {
            var orderline = args.Order.OrderLines.FirstOrDefault(x => x.Id == orderKey);
            if (orderline == null)
            {
                continue;
            }

            var organisationId = args.Order.Properties.GetValueOrDefault(CommerceConstants.Properties.OrganisationId);
            if (organisationId == null)
            {
                // No organisation ID, so no organisation based discount.
                continue;
            }

            decimal netPrice = calculation.UnitPrice.Value.WithoutTax;

            var priceCommand = new ProductPriceCommand(orderline.Sku) { OrganisationId = organisationId };
            var adjustments = wholesaleStrategy.MaybeGetPriceAdjutments(priceCommand, netPrice).GetAwaiter().GetResult();

            foreach (var adjustment in adjustments)
            {
                decimal grossPrice = calculation.UnitPrice.Value.WithTax;
                decimal taxAmount = grossPrice - netPrice;
                decimal taxRate = taxAmount / netPrice;

                decimal discountNet = adjustment.Amount;
                decimal discountTax = discountNet * taxRate;

                var price = new Price(adjustment.Amount, discountTax, args.Order.CurrencyId);

                var myAdjustment = new OrganisationBasedAdjustment(organisationId, price);
                // Clear any existing adjustments for this order line
                args.OrderLines[orderKey].UnitPriceAdjustments.Clear();
                for (int i = 0; i < orderline.Quantity; i++)
                {
                    args.OrderLines[orderKey].UnitPriceAdjustments.Add(myAdjustment);
                }
            }
        }
    }
}

This works as I expect for 1 unit in an orderline:

However with 10:

& with 5:

I checked the table storing the adjustments just to be double sure there’s not something with database’s going on and when the quantity is 5, I expected to see 5 adjustments (one per unit).

I’m thinking I should not be manipulating the quantity and applying per unit, but I’ve got such bad tunnel vision I’m not overly sure I’ve even done this correctly to start with.

I’ve read the documentation: Price/Amount Adjustments | Umbraco Commerce but there isn’t anything specific about the unit price adjustment, just the overall subtotal of the order.

So yea, a unit price adjustment applies before the multiplication of quantity, so you shouldn’t need to be adding adjustments per quantity. The orderline total price should be unit price (inc adjustments) * quantity.

Only other thing I would suggest is to make sure you adjustments (if discounts) are a negative value. With adjustments a price can be negative (a discount) or positive (a fee)