1

I am observing controller_action_postdispatch_checkout_cart_add, in some cases if customer adds a product we have to reset to quantity 1

$productId= $this->_request->getParam('product');
        $productAdded = $this->_productRepository->getById($productId);
$item= $quote->getItemByProduct($productAdded);
            $item_id= $item->getItemId();

            $params[$item_id]['qty'] = 1;
            $params = array(
                'product' => $productId,
                'qty'     => 1,
            );
            echo $item_id;
            $quote->updateItem($item_id,$params);

But updateItem expects 3 parameters

public function updateItem($itemId, $buyRequest, $params = null)

What would be $buyRequest in my case

or there is another way to update quantity in cart

2 Answers 2

2

used controller_action_postdispatch_checkout_cart_add event

<?php

namespace Mycompany\Inventoryrationing\Observer;

use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Catalog\Model\ProductFactory;
use \Magento\Framework\App\RequestInterface;
use Magento\Catalog\Model\ProductRepository;
use \Magento\Checkout\Model\Cart;
use \Magento\Framework\Message\ManagerInterface ;

use \Magento\Checkout\Model\Session as CheckoutSession;

class Beforeaddtocart implements ObserverInterface {
    /** @var CheckoutSession */
    protected $checkoutSession;
    /**
     * @var ProductFactory
     */
    protected $_productloader;
    /**
     * @var ProductRepository
     */
    protected $_productRepository;
    /**
     * @var RequestInterface
     */
    protected $_request;
    /**
     * @var Rationinglogic|Data
     */
    protected $_helperData;
    /**
     * @var Cart
     */
    protected $_cart;
    /**
     * @var ManagerInterface
     */
    protected $_messageManager;

    /**
     * @param CheckoutSession $checkoutSession
     * @param ProductFactory $productLoader
     * @param RequestInterface $request
     * @param ProductRepository $productRepository
     * @param Cart $cart
     * @param ManagerInterface $messageManager
     */
    public function __construct(
        CheckoutSession $checkoutSession,
        ProductFactory $productLoader,
        RequestInterface $request,
        ProductRepository $productRepository,
        cart $cart,
        ManagerInterface $messageManager

    ) {
        $this->checkoutSession = $checkoutSession;
        $this->_productloader = $productLoader;
        $this->_request = $request;
        $this->_productRepository = $productRepository;
        $this->_helperData = $helperData;
        $this->_cart = $cart;
        $this->_messageManager = $messageManager;
    }

    /**
     * After cart observer
     * @param Observer $observer
     * @return Cart
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function execute(Observer $observer) {
        $productId= $this->_request->getParam('product');
        $productAdded = $this->_productRepository->getById($productId);
        $productSku = $productAdded->getSku();
        $mappedProductSkus= $this->_helperData->getSku();
        $quote = $this->checkoutSession->getQuote();
        if(Condition)
        {
            $item= $quote->getItemByProduct($productAdded);
            $itemId= $item->getItemId();
            $params[$itemId]['qty'] = 1;
            $this->_cart->updateItems($params);
            $this->_cart->saveQuote();
            $this->_cart->save();
            $this->_messageManager->addNoticeMessage('Only one item can be bought at a time');
            return true;
        }
    }

}
1

You may alter qty on cart_product_add_after observer

$quoteItem = $observer->getEvent()->getQuoteItem();
$avl_qty = 1; 
if ($avl_qty == '0') {
  $quoteItem->getQuote()->removeItem($quoteItem->getItemId());
  throw new LocalizedException(__("This product is currently out of stock."));                    
} 
elseif ($order_qty > $avl_qty) {                   
  $quoteItem->setData('qty', $avl_qty);
  $this->_cart->save();
  $this->messageManager->addNoticeMessage('Sorry we have only '.$avl_qty.' qty of this product available');                    
} 
else {                   
} 

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.