Skip to content

Magento2 Add To Cart before plugin not working: 2.4.6

my plugin does not work. I am unable to load products onto the cart There are no errors in the error log.

My Di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoCheckoutModelCart">
            <plugin name="addingProductToCart"
                    type="ExampleTestPluginModelCheckoutCartPlugin"
                    sortOrder="10"
                    disabled="false"/>
        </type>
    </config>

my Plugin

public function beforeAddProduct($product,$request)
    {
        try {

            // Your custom code here.
        } catch (Exception $e) {
            throw new LocalizedException(__($e->getMessage()));
        }

        return [$product, $request];
    }

Please note, a lot of the tutorials return 3 params, but the current cart class only return two:

vendor/magento/module-checkout/Model/Cart.php

class Cart extends DataObject implements CartInterface
{
     public function addProduct($productInfo, $requestInfo = null)
        {
            $product = $this->_getProduct($productInfo);
            $productId = $product->getId();
          ......
    $this->_eventManager->dispatch(
                'checkout_cart_product_add_after',
                ['quote_item' => $result, 'product' => $product]
            );
            $this->_checkoutSession->setLastAddedProductId($productId);
            return $this;

    }

}