1

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="Magento\Checkout\Model\Cart">
            <plugin name="addingProductToCart"
                    type="Example\Test\Plugin\Model\Checkout\Cart\Plugin"
                    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;

    }

}
1
  • Kindly check Rahul's answer. I have already used the below code and it's working fine. I have the same answer so not adding my answer. Jul 10, 2023 at 13:56

2 Answers 2

1

In this example, we will define a plugin class that observed function addProduct($productInfo, $requestInfo = null) of a Magento\Checkout\Model\Cart class. addProduct method call each and every time when we add product to cart.In this example, we will change product quantity during add to cart.

Step-1: Create di.xml at Vendor/Module/etc/di.xml. This is declaration of plugin.

<?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="Magento\Checkout\Model\Cart">
     <plugin name="productcart" type="Vendor\Module\Plugin\Productcart" sortOrder="1" />
 </type>

Step-2: Create Productcart.php as plugin class at Vendor\Module\Plugin\Productcart.php and addProduct method written as beforeAddProduct method in plugin class.

<?php

namespace Vendor\Module\Plugin;

class Productcart
{
    public function beforeAddProduct(
                    \Magento\Checkout\Model\Cart $subject,
                    $productInfo,
                    $requestInfo = null
            ) {
        
                $requestInfo['qty'] = 5; // increasing quantity to 5
                return array($productInfo, $requestInfo);
    }
}
0

Please define the plugin in your di.xml

<type name="Magento\Checkout\Model\Cart">
   <plugin name="add_to_cart_plugin" type="Vendor\Module\Plugin\BeforeAddToCart"/>
</type>

Now define your plugin class

<?php

namespace Vendor\Module\Plugin;

use Magento\Checkout\Model\Cart;

class BeforeAddToCart
{
    public function beforeAddProduct(Cart $subject, $productInfo, $requestInfo = null)
    {
        (execute your code...)

        return [$productInfo,$requestInfo];
    }
}

Please try this and let me know if any issues.

3
  • i think your code is old and incorrect. the Cart is deprecated. Also its now two params, not three Jul 10, 2023 at 13:13
  • ok i will send you updated code with testing. Jul 10, 2023 at 13:14
  • Thanks. please. i am using the latest magento version 2.4.6 Jul 10, 2023 at 13:19

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.