1

Sometimes the mini cart show "You have no items in your shopping cart." but indeed there are some items in cart. I wrote a function to refresh the mini cart in order to avoid the problem above.

I tried to modify and use the core functions below to refresh it but without success. Magento\Checkout\Observer\LoadCustomerQuoteObserver:

public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $this->checkoutSession->loadCustomerQuote();
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('Load customer quote error'));
        }
    }

Magento\Checkout\Controller\Cart\UpdatePost:

protected function _updateShoppingCart()
    {
        try {
            $cartData = $this->cart->getQuote()->getItemsCollection()->getData();
//            $cartData = $this->getRequest()->getParam('cart');
            if (is_array($cartData)) {
                $filter = new \Zend_Filter_LocalizedToNormalized(
                    ['locale' => $this->objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()]
                );
                foreach ($cartData as $index => $data) {
                    if (isset($data['qty'])) {
                        $cartData[$index]['qty'] = $filter->filter(trim($data['qty']));
                    }
                }
                if (!$this->cart->getCustomerSession()->getCustomerId() && $this->cart->getQuote()->getCustomerId()) {
                    $this->cart->getQuote()->setCustomerId(null);
                }

                $cartData = $this->cart->suggestItemsQty($cartData);
                $this->cart->updateItems($cartData)->save();
                error_log('finished update cart');
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError(
                $this->objectManager->get('Magento\Framework\Escaper')->escapeHtml($e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('We can\'t update the shopping cart.'));
            $this->objectManager->get('Psr\Log\LoggerInterface')->critical($e);
        }
    }

How can I refresh the mini-cart? Thanks in advance.

  • Anyone can help? – Ricky.C Jul 29 '16 at 1:40
  • have you resolved? – Bojjaiah Jan 6 '17 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.