nystudio107/craft-instantanalytics

Remove from cart events are never triggered

Closed this issue · 1 comments

I'm not sure exactly what's going on here, but this defined check never evaluates truthfully. I've never seen this syntax for checking if a static property exists, so I'm thinking it's an error?

if (defined('Order::EVENT_AFTER_REMOVE_LINE_ITEM')) {
Event::on(Order::class, Order::EVENT_AFTER_REMOVE_LINE_ITEM, function (LineItemEvent $e) {
$lineItem = $e->lineItem;
if (self::$settings->autoSendRemoveFromCart) {
$this->commerce->removeFromCart($lineItem->order, $lineItem);
}
});

Removing the check allows the events to trigger as expected.

Craft Commerce: 3.1.12 Pro

So this is actually a constant, not a static property. I'm checking if this exists as per:

https://stackoverflow.com/questions/24159178/check-if-a-class-constant-exists

But I'm thinking that perhaps it needs to be a fully qualified class name to work as expected.

I've changed it to:

            // Check to make sure Order::EVENT_AFTER_REMOVE_LINE_ITEM is defined
            if (defined(Order::class . '::EVENT_AFTER_REMOVE_LINE_ITEM')) {
                Event::on(Order::class, Order::EVENT_AFTER_REMOVE_LINE_ITEM, function (LineItemEvent $e) {
                    $lineItem = $e->lineItem;
                    if (self::$settings->autoSendRemoveFromCart) {
                        $this->commerce->removeFromCart($lineItem->order, $lineItem);
                    }
                });
            }

Can you try it out and lmk if this solves it for you? Change your semVer to dev-develop -> afa461f