1

Events.xml

<event name="controller_action_postdispatch_customer_account_loginPost">
       <observer name="customer_login_observer" instance="vendor\module\Observer\Customercart" />
 </event>

Customercart.php

<?php

namespace vendor\module\Observer;

class Customercart implements \Magento\Framework\Event\ObserverInterface
{
public function __construct(
         \Magento\Framework\Message\ManagerInterface $messageManager,
         \Magento\Checkout\Model\Session $session,
     \Magento\Customer\Model\Session $customerSession,
        )
    {
         $this->_session = $session;
    $this->_customerSession = $customerSession;
         $this->_messageManager = $messageManager;
    }
public function execute(\Magento\Framework\Event\Observer $observer)
{
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/teqwewq.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        if($this->_customerSession->isLoggedIn()){
          $customerId=$this->_customerSession->getCustomer()->getId();
        }
        $currentQuote=$this->_session->getQuote();
        $currentQuoteItems=$currentQuote->getAllVisibleItems();
        $currentQuoteItemSku = array();
        foreach ($currentQuoteItems as $currentQuoteItem) {
                   array_push($currentQuoteItemSku, $currentQuoteItem->getSku());
                   $logger->info($currentQuoteItem->getSku());
                }
          $logger->info("-------");
        $clbStatus=false;$normalProductStatus=false;
        foreach($currentQuoteItemSku as $value){
          if($value=="some1" || $value=="some2"){
            $clbStatus=true;
          }
          else{
            $normalProductStatus=true;
          }
        }
        $logger->info($normalProductStatus);
        $logger->info($clbStatus);
        $currentQuote->setTotalsCollectedFlag(false)->save();
        if($clbStatus && $normalProductStatus){
        foreach ($currentQuoteItems as $currentQuoteItem) {
                        if($currentQuoteItem->getSku()=="some1" ||$currentQuoteItem->getSku()=="some2"){
                            $itemId = $currentQuoteItem->getItemId();
                            $currentQuote->deleteItem($currentQuoteItem);//->save();
                        }
                }
        }
        $currentQuote->collectTotals()->save();
        return $this;

}
}

Screen shot ref https://nimb.ws/qSPujg

The subtotal does not change on the cart and also in the window.checkoutConfig.quoteData.base_grand_total

eg

A product $40

B product $20 // removed

subtotal $60 //remains same - after a refresh , even go to checkout

Any suggestion?

2 Answers 2

4

Try to add $currentQuote->setTriggerRecollect(1); before your collectTotals().

It will be :

$currentQuote->setTriggerRecollect(1);
$currentQuote->collectTotals()->save();
2
  • Thanks, it works.
    – Alen
    Jan 29, 2020 at 7:20
  • I am facing same issue but the given solution is not working for me.
    – Team18
    Oct 17, 2020 at 16:36
0

If above code doesn't work then use below as well.

$currentQuote= $this->_checkoutSession->getQuote();

$shippingAddress = $currentQuote->getShippingAddress();

$shippingAddress->setCollectShippingRates(true)->collectShippingRates();

$currentQuote->setTriggerRecollect(1);

$currentQuote->collectTotals();

$currentQuote->save($currentQuote);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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