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.
Craft::dd()
in your event listener? – Luke Holder Aug 7 '19 at 10:27