0

How to multiply the quantity of the added product by 2 by clicking the add to cart button through the before plugin?

1 Answer 1

0

this can be achieved with a Plugin:

In your module

on app\code\vendor\module\etc\events.xml

put this

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch_checkout_cart_add">
        <observer name="restrict_sales_model_cart_add_before" instance="vendor\module\Observer\CartAddingConditionCheck" />
    </event>  
</config>

In your app\code\vendor\module\Observer create the file CartAddingConditionCheck.php and place the following code.

<?php
namespace vendor\module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Checkout\Model\Cart;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\Product;
use \Magento\Customer\Model\Session as customerSession;

class CartAddingConditionCheck implements ObserverInterface
{
    protected $cart;
    protected $messageManager;
    protected $redirect;
    protected $request;
    protected $product;
    protected $customerSession;
    protected $productloader;
    protected $quoteRepository;
    protected $customerRepositoryInterface;
    protected $productRepository;
    protected $storeManager;
    
    public function __construct(
       RedirectInterface $redirect,
       Cart $cart,
       ManagerInterface $messageManager,
       RequestInterface $request,
       Product $product,
       customerSession $session,
       \Magento\Catalog\Model\ProductFactory      $productloader,
       \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
       \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
       \Magento\Catalog\Model\ProductRepository         $productRepository,
       \Magento\Store\Model\StoreManagerInterface $storeManager
       
    )
    {
        $this->redirect        = $redirect;
        $this->cart            = $cart;
        $this->messageManager  = $messageManager;
        $this->request         = $request;
        $this->product         = $product;
        $this->customerSession = $session;
        $this->productloader   = $productloader;
        $this->quoteRepository = $quoteRepository;
        $this->customerRepositoryInterface = $customerRepositoryInterface;
        $this->productRepository  = $productRepository;
        $this->storeManager    = $storeManager;
    }
    
    public function execute(
        \Magento\Framework\Event\Observer $observer
    )
    {   
        $postValues = $this->request->getPostValue();
        $item = $this->product;   
        if ($item->getParentItem()) 
        {
            $item = $item->getParentItem();
        }
        $quantity         = isset($postValues['qty']) ? $postValues['qty'] : 1;
        $item->setQty($quantity * 2);
    }
}
?>

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.