1

I have a product where I want to charge the full price ($300) for the first one, and then a discounted amount ($50) for each additional one.

So if a customer orders one, the cost is $300. Two would be $350. Three would be $400, etc.

I know I can set a discount that applies to all items, but is there a way to only apply it after the first?

1 Answer 1

1

You need to add the adjuster to your logic via a module/plugin:

And the main body of your adjuster should look like this (if I correctly read your logic):

<?php

namespace modules\mymodule\adjusters;

use craft;
use craft\base\Component;
use craft\commerce\base\AdjusterInterface;
use craft\commerce\elements\Order;
use craft\commerce\models\OrderAdjustment;

class CustomAdjuster extends Component implements AdjusterInterface
{
    /**
     * @param Order $order
     *
     * @return array|OrderAdjustment[]
     */
    public function adjust(Order $order): array
    {
        $adjustments = [];

        $lineItems = $order->getLineItems();

        foreach ($lineItems as $i => $item) {
            // First product? Skip it
            if (0 === $i) {
                continue;
            }
            
            $adjustment = new OrderAdjustment();
            $adjustment->type = 'discount';
            $adjustment->name = "My 50$ discount";
            $adjustment->description = "My 50$ discount";
            // `sourceSnapshot` can contain information to explain the adjustment
            $adjustment->sourceSnapshot = [ 'data' => 'value' ];
            $adjustment->amount = 50;
            $adjustment->setOrder($order);
            $adjustment->setLineItem($item);
            $adjustments[] = $adjustment;
        }

        return $adjustments;
    }
}
1
  • That will discount everything in my cart after the first item, but I need it to only apply to a certain product variant. The logic needs to be along the lines of if product id = 50, and quantity is > 1, then apply the discount to everything after the first.
    – artmem
    Feb 16, 2023 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.