0

I am trying to update all the quote item qty as soon as we navigated to the cart page.

by using below code.

$quote = $this->checkoutSession->getQuote();
$quoteItems = $quote->getAllVisibleItems();
                foreach($quoteItems as $item) {
                        $productSku = $item->getSku();
                        $item->setQty(1);
                        $item->save();
                }

The above code working fine if we run from the script for or from any of the controller file.

I am looking for code to implement in event and observers,

Is there any event for this, once navigated to cart page i need update qty for each item as 1.

Can anyone suggest which event is worth for it.

Thanks

2

You can use the Controller dispatch event of Cart page controller_action_predispatch_checkout_cart_index.

On this event fire an observer and update the Cart Qty.

Observer

<?php

namespace StackExchange\Magento\Observer\CheckoutCart;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Exception\LocalizedException;

class UpdateQty implements \Magento\Framework\Event\ObserverInterface
 {
    /**
     * @var \Magento\Quote\Api\CartRepositoryInterface
     */
    protected $quoteRepository;
    /**
     * @var CheckoutSession
     */
    private $checkoutSession;
    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    public function __construct(
     \Psr\Log\LoggerInterface $logger,
     CheckoutSession $checkoutSession,
     \Magento\Quote\Api\CartRepositoryInterface $quoteRepository

    ) {
        $this->checkoutSession = $checkoutSession;
        $this->logger = $logger;
    }
    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $this->logger->debug(__METHOD__);
        $quote = $this->checkoutSession->getQuote();
        $quoteItems = $quote->getAllVisibleItems();
        if(!count($quoteItems) <= 0){
            return  $this;
        }
        try {

            foreach($quoteItems as $item) {
                    $productSku = $item->getSku();
                    $item->setQty(1);
                    $item->save();
            }
           $this->quoteRepository->save($quote); 
        } catch (LocalizedException $e) {
            $this->logger->critical($e->getMessage());
        } catch (\Exception $e) {
            $this->logger->critical($e->getMessage());
        }

    }

}
  • hi @Amit, Thanks, the event is fired, i have used below code to set the custom price of each item, $customPrice = 50; $item->setCustomPrice($customPrice); $item->setOriginalCustomPrice($customPrice); $item->getProduct()->setIsSuperMode(true);, is this fine? – jafar pinjar Jun 10 at 14:07
  • can you confirm me please? – jafar pinjar Jun 10 at 14:33
  • You can do but it is very bad idea to change the price at cart page using event. Best idea add an intermediate controller and impment this logic – Amit Bera Jun 10 at 14:35
  • intermediate controller means, how you are saying? any suggestions? – jafar pinjar Jun 10 at 14:36
  • intermedia controller means hitting an intermediate URL before goto cart page – Amit Bera Jun 10 at 14:47

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.