1

I want to update qty to 2 when product is added to Cart. Basically I want that when a user ads a product to cart(qty is not visible for user), it is set to qty by default. I have created an observer and using event checkout_cart_product_add_after

But when I try to add product to cart, somehow the CPU Usage % goes like 90% and server crashes and I have to restart apache. My code looks like -

namespace Referral\Duplicate\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Checkout\Model\Session as CheckoutSession;


class DuplicateObserver implements ObserverInterface
{
    private $_checkoutSession;
    protected $_productRepository;
    protected $_cart;
    protected $formKey;
    public function __construct(
        \Magento\Catalog\Model\ProductRepository $productRepository, 
        \Magento\Checkout\Model\Cart $cart, 
        \Magento\Framework\Data\Form\FormKey $formKey,
        CheckoutSession $checkoutSession,
        \Magento\Catalog\Model\Product $product,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        \Magento\Quote\Model\Quote\Item $item

    )
    {
        $this->quoteRepository = $quoteRepository;
        $this->_productRepository = $productRepository;
        $this->_cart = $cart;
        $this->formKey = $formKey;
        $this->product = $product; 
        $this->_checkoutSession = $checkoutSession;
    }

    public function execute(EventObserver $observer)
    {
        $couponCode = 'duplicate';    
        $items = $observer->getQuoteItem();
        $product = $observer->getEvent()->getData('product');
        $productId = $product->getId();

        $skuQty = count($observer->getQuoteItem());
        $cartItems = $this->_cart->getQuote()->getAllVisibleItems();
        $quote = $this->_checkoutSession->getQuote();
        $_product = $this->_productRepository->getById($productId);

        $params = array(
                    'form_key' => $this->formKey->getFormKey(),
                    'product' => $productId, 
                    'qty'   => 1
                );              
        // //$newProduct = $this->product->load($productId);       
        //$this->_cart->addProduct($product, $params);
        $this->_cart->save();


    }

}

Issue comes when I uncomment line -

// //$newProduct = $this->product->load($productId);       
        //$this->_cart->addProduct($product, $params);
0

You can try the following code to achieve your requirement: Use the below for events.xml

<?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="cart_update_custom" instance="Vendor\Module\Observer\UpdateCartItem" />
    </event>
</config>

And use the below code UpdateCartItem.php:

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Checkout\Model\Cart;

class UpdateCartItem implements ObserverInterface {
    /**
     * @var Cart
     */
    protected $_cart;

    public function __construct(
        cart $cart

    ) {
        $this->_cart = $cart;
    }

    /**
     * After cart observer
     * @param Observer $observer
     * @return Cart
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function execute(Observer $observer) {
        $quoteItem = $observer->getEvent()->getQuoteItem();
        $quoteItem->setData('qty', 4);
        $this->_cart->save();
    }

}

Run di compile and test.

| improve this answer | |
  • I do not want to set minimum to 2. This event I will use for specific scenario hence I have created code. – Gagan Jan 8 at 14:35
  • Check my updated answer. – Sukumar Gorai Jan 8 at 15:15

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.