0

I want to add the double quantity when I click add to cart button using event in Magento2.3.2

which function I can override for this.

Thanks in advance.

0

Here we will see how to set double Quantity of product in magento2. You can change product quantity when adding product to cart. You can achieve this by Observer.

First create events.xml file in folder ‘Webkul/Hello/etc/frontend’ and use event ‘checkout_cart_product_add_after’.

<?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="checkout_cart_product_add_after">
        <observer name="customprice" instance="VendorName\ModuleName\Observer\CustomQty" />
    </event>
</config>

Now create CustomQty.php file in Observer folder.

<?php
    namespace VendorName\ModuleName\Observer;

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;

    class CustomQty implements ObserverInterface
    {
        public function execute(\Magento\Framework\Event\Observer $observer) {
            $item = $observer->getEvent()->getData('quote_item');           
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            $qty= $item->getQty() * 2; //set your quantity here
            $item->setQty($qty);
            $item->getProduct()->setIsSuperMode(true);
        }

    }

For cart page or minicart you have to override Magento\Checkout\Controller\Cart\UpdateItemQty controller file through plugin or preference you can see this link how to override controller

https://www.mageplaza.com/magento-2-module-development/magento-2-how-to-rewrite-controller.html

in your overridden controller file replace updateItemQuantity function to

private function updateItemQuantity(Item $item, float $qty)
    {
        if ($qty > 0) {
            $item->clearMessage();
            $item->setQty($qty * 2);

            if ($item->getHasError()) {
                throw new LocalizedException(__($item->getMessage()));
            }
        }
    }
3
  • It's working for me, but In cart page when i update the Quantity link 3 and update, it's accept my quantity 3. Can you give me the solution for that. – Meera Feb 6 '20 at 6:39
  • Please see I have edited my answer. – user43082 Feb 6 '20 at 7:07
  • 1
    Now It's working. – Vrajesh Patel Feb 6 '20 at 7:41
0

You can create observer to update quantity in cart items.

public function execute(Observer $observer)
{

    $cart = $observer->getData('cart');
    $quote = $cart->getData('quote');
    $items = $quote->getAllVisibleItems();

    foreach($items as $item)
    {   
            $item->setQty(2);
            $item->save();   
    }
    $quote->save();
}

Check this out for details.

Hope this works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.