1

I have created a custom adjuster, there is a surcharge per item if a customer has selected rush delivery. But i need to charge tax on that adjuster. Below is code:

public function adjust(Order $order): array
{
    $adjustments = [];

    foreach ($order->getLineItems() as $item) {
        $adjustment = new OrderAdjustment;
        $adjustment->type = 'rush-delivery';
        $adjustment->name = 'Rush Delivery';
        $adjustment->description = 'Rush Delivery';
        $adjustment->sourceSnapshot = ['data' => 'value']; // This can contain information about how the adjustment came to be
        $adjustment->amount = 1;
        $adjustment->setOrder($order);
        $adjustment->setLineItem($item);

        $adjustments[] = $adjustment;
    }

    return $adjustments;
}

But tax is not added to the £1 i am adding here. How can i get tax to be added or calculated as included?

3

Having slept on it i thought it could be something to do with the order of the adjusters. Maybe Tax was applying to the price before my custom adjuster. So in the event i did the following:

var_dump($event->types)

This told me that my custom adjuster was going to the bottom of this array. To resolve this i used the php function array_unshift to push it to the front of the array

array_unshift($event->types,MyAdjuster::class);

This now gave me the following array:

array(4) {
      [0]=>
      string(18) "modules\MyAdjuster"
      [1]=>
      string(33) "craft\commerce\adjusters\Shipping"
      [2]=>
      string(33) "craft\commerce\adjusters\Discount"
      [3]=>
      string(28) "craft\commerce\adjusters\Tax"
    }

As you can see the adjuster is at the front of the array and now the tax applies to the fee as required.

1
  • When you say "in the event" do you mean EVENT_REGISTER_ORDER_ADJUSTERS? Mar 6 '20 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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