Commerce: Remove tax adjuster from line item in plugin I need to remove adjusters from each line item if a certain condition is not met. I see that in the CP you can manually remove them by editing the line item but hunting down this code in Commerce's codebase has proven difficult.


Here's some of my code. I'm intending to keep certain adjusters from appearing for all line items before they save. How can I do this?

use yii\base\Event;
use craft\commerce\services\LineItems;
use craft\commerce\events\LineItemEvent;
use craft\commerce\models\OrderAdjustment;
use craft\commerce\models\LineItem;

Event::on(
    LineItems::class,
    LineItems::EVENT_BEFORE_SAVE_LINE_ITEM,
    function(LineItemEvent $event) {

        $lineItem = $event-&gt;lineItem;
        foreach ($lineItem-&gt;getAdjustments() as $adjustment) {
            /* @var OrderAdjustment $adjustment */

            if ($someExampleCondition) {
                // remove adjustment
            }

        }

    });


But when I inspect an OrderAdjustment model or LineItem model, I don't see any methods about removing or setting adjusters, only getting them.
There is a way to do this, obviously, it is worth noting that the code below is written to be easy to understand and could be improved. Also, it is worth noting that there are plenty of things to think about when implementing something like this.

Take careful consideration about how this will affect all parts of the e-commerce experience, whether that is carts, orders, adding to cart in the front end and cart management in the control panel.

The following code will remove a tax adjustment based on the SKU of the line item and the name of the tax adjustment, your criteria may obviously vary.

use craft\commerce\elements\Order;
use craft\commerce\Plugin;
use craft\events\ModelEvent;
// ...

Event::on(Order::class,
    Order::EVENT_AFTER_SAVE,
    function(ModelEvent $event) {
        $lineItems = $event-&gt;sender-&gt;getLineItems();
        $adjustments = $event-&gt;sender-&gt;getAdjustments();

        // No need to do anything if this is an order or there aren't any line items or adjustments
        if (empty($lineItems) || empty($adjustments) || $event-&gt;sender-&gt;isCompleted) {
            return;
        }

        // Figure out which line items we want to remove tax from
        $removeTaxFromLineItemIds = [];
        foreach ($lineItems as $lineItem) {
            if ($lineItem-&gt;getSku() == 'MY-EXAMPLE-SKU') {
                $removeTaxFromLineItemIds[] = $lineItem-&gt;id;
            }
        }

        if (empty($removeTaxFromLineItemIds) ) {
            return;
        }

        // Loop through adjustments and remove the tax adjustments for the line items we determined above
        $taxNameToRemove = 'My Tax';
        foreach ($adjustments as $key =&gt; $adjustment) {
            if (in_array($adjustment-&gt;lineItemId, $removeTaxFromLineItemIds) &amp;&amp; $adjustment-&gt;type === Tax::ADJUSTMENT_TYPE &amp;&amp; $adjustment-&gt;name == $taxNameToRemove) {
                unset($adjustments[$key]);
                Plugin::getInstance()-&gt;getOrderAdjustments()-&gt;deleteOrderAdjustmentByAdjustmentId($adjustment-&gt;id);
            }
        }

        $event-&gt;sender-&gt;setAdjustments($adjustments);
    }
);


This code can be used inside a custom plugin or module.

Hope this helps.