2

Following https://docs.craftcms.com/commerce/v2/adjusters.html I am unable to register an adjuster in my module.

in my Module.php file I have placed the following inside init() alongside other stuff being registered (that are working)

use craft\events\RegisterComponentTypesEvent;
use craft\commerce\services\OrderAdjustments;
use modules\mymodule\adjusters\DiscountAdjuster;

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

And in src/adjusters/DiscountAdjuster.php I have

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 DiscountAdjuster 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';
            $adjustment->sourceSnapshot = [ 'data' => 'value']; // This can contain information about how the adjustment came to be
            $adjustment->amount = -2;
            $adjustment->setOrder($order);
            $adjustment->setLineItem($item);

            $adjustments[] = $adjustment;
        }

        return $adjustments;
    }
}

This should take 2 off of every line item when I add items to the cart, no? Nothing happens though. And if i put something like die() in public function adjust, that isn't registered either.

What am I missing?

I also tried adapting this example: Demo repo for an Adjuster module for Commerce 2? However I'm faced with the same issue.

  • Can you log something or Craft::dd() in your event listener? – Luke Holder Aug 7 at 10:27
  • I am almost curtain the module has not been bootstrapped. I had the same issue but have a Yii background and found the issue pretty quick. – Dean Sanderson Aug 7 at 16:55
1

Have you made sure the module has bootstrapped? Check the app config and uncomment the bootstrap line

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.