1

I'm attempting to add a very basic tax adjuster to specific users based on a custom lightswitch field. I currently have a simple plugin with two classes for the plugin and adjuster that mirror the example in the adjuster documentation.

craft/plugins/taxexemptadjuster/TaxExemptAdjusterPlugin.php

namespace Craft;

require_once('Adjusters/TaxExempt_TaxAdjuster.php');

class TaxExemptAdjusterPlugin extends BasePlugin
{
    ...

    public function commerce_registerOrderAdjusters()
    {
        return [
            601 => new \Commerce\Adjusters\TaxExempt_TaxAdjuster
        ];
    }
}

craft/plugins/taxexemptadjuster/Adjusters/TaxExempt_TaxAdjuster.php

namespace Commerce\Adjusters;

use Craft\Commerce_LineItemModel;
use Craft\Commerce_OrderAdjustmentModel;
use Craft\Commerce_OrderModel;

class TaxExempt_TaxAdjuster implements Commerce_AdjusterInterface
{
    public function adjust(Commerce_OrderModel &$order, array $lineItems = [])
    {
        $taxAdjuster = new Commerce_OrderAdjustmentModel();

        if (craft()->userSession->getUser()->getContent()->getAttribute('userTaxExempt')) {
            $order->baseTax = $order->baseTax - $order->baseTax;

            $taxAdjuster->type = "Tax";
            $taxAdjuster->name = "Tax Exempt";
            $taxAdjuster->description = "Tax Exempt";
            $taxAdjuster->amount = -$order->baseTax;
            $taxAdjuster->orderId = $order->id;
            $taxAdjuster->optionsJson = ['lineItemsAffected' => null];
            $taxAdjuster->included = false;

            return [$taxAdjuster];
        }

        return [];
    }
}

Based on the examples I've found in the adjusters documentation this should zero out the baseTax and totalTax values if an authenticated user has the custom userTaxExempt lightswitch field enabled. I've tried removing the conditional to see if there was an issue with the custom field on the user profile but even then none of the adjustments are applied.

What have I overlooked?

2 Answers 2

1

I'm not sure but this jumped out at me

$myAdjuster->amount = -$order->totalTax;

I don't think you can "negative" a variable in that way.

Try $myAdjuster->amount = $order->totalTax * -1; instead

1

It seems you're setting the adjuster $taxAdjuster->amount to zero. This should be the correct adjuster code...

namespace Commerce\Adjusters;

use Craft\Commerce_LineItemModel;
use Craft\Commerce_OrderAdjustmentModel;
use Craft\Commerce_OrderModel;

class TaxExempt_TaxAdjuster implements Commerce_AdjusterInterface
{
    public function adjust(Commerce_OrderModel &$order, array $lineItems = [])
    {
        $taxAdjuster = new Commerce_OrderAdjustmentModel();

        if (craft()->userSession->getUser()->getContent()->getAttribute('userTaxExempt')) {
            $originalTax = $order->baseTax;
            $order->baseTax = 0;

            $taxAdjuster->type = "Tax";
            $taxAdjuster->name = "Tax Exempt";
            $taxAdjuster->description = "Tax Exempt";
            $taxAdjuster->amount = $originalTax;
            $taxAdjuster->orderId = $order->id;
            $taxAdjuster->optionsJson = ['lineItemsAffected' => null];
            $taxAdjuster->included = false;

            return [$taxAdjuster];
        }

        return [];
    }
}

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.