0

I created a new plugin to register an order adjustment to remove tax for Tax Exempt customers, but I'm having troubles getting the adjustment to actually take place.

I have a main plugin file as Adjusters.php

<?php
/**
 * Adjusters plugin for Craft CMS 3.x
 *
 * Custom adjusters
 *
 * @link      https://edencreative.co
 * @copyright Copyright (c) 2020 Eden Creative
 */

namespace edencreative\adjusters;


use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\UrlManager;
use craft\events\RegisterUrlRulesEvent;
use craft\commerce\services\OrderAdjustments;
use craft\events\RegisterComponentTypesEvent;
use edencreative\adjusters\adjusters\TaxRemover;

use yii\base\Event;

/**
 * Class Adjusters
 *
 * @author    Eden Creative
 * @package   Adjusters
 * @since     1.0.0
 *
 */
class Adjusters extends Plugin
{
    // Static Properties
    // =========================================================================

    /**
     * @var Adjusters
     */
    public static $plugin;

    // Public Properties
    // =========================================================================

    /**
     * @var string
     */
    public $schemaVersion = '1.0.0';

    // Public Methods
    // =========================================================================

    /**
     * @inheritdoc
     */
    public function init()
    {

        Event::on(
            OrderAdjustments::class,
            OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS,
            function(RegisterComponentTypesEvent $event) {
                $event->types[] = TaxRemover::class;
            }
        );
    }

    // Protected Methods
    // =========================================================================

}

Then my adjuster in plugins/src/adjusters/TaxRemover.php

<?php

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

class TaxRemover extends Component implements AdjusterInterface
{
    public function adjust(Order $order): array
    {
        $adjustments = [];

        foreach ($order->getLineItems() as $item) {
            $adjustment = new OrderAdjustment;
            $adjustment->type = 'discount';
            $adjustment->name = '$2 off';
            $adjustment->description = '$2 off everything in the store';
            // `sourceSnapshot` can contain information to explain the adjustment
            $adjustment->sourceSnapshot = [ 'data' => 'value' ];
            $adjustment->amount = -2;
            $adjustment->setOrder($order);
            $adjustment->setLineItem($item);

            $adjustments[] = $adjustment;
        }

        return $adjustments;
    }
}
  • 1
    Do you have other code in this plugin that you know is executing? Definitely worth dropping a Craft::dd('Hello, world!') in your plugin's init() method. Keep in mind that orders have to be re-saved in order to have Adjustments recalculated, so if you're just refreshing a cart, it might not display immediately! – August Miller Mar 27 at 19:21

Your Answer

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

Browse other questions tagged or ask your own question.