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

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

It will be :

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

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.