1

Sometimes mini cart showing the wrong count. So is there any possible ways to forcefully update the mini cart count.

The customer can select one free sample item when grand total greater than 1500 otherwise no. After selecting free sample item if customer update with less than Qty on that time Grand total is less than 1500 I am removing the free sample item. It's removing successfully but the count is showing wrong.

UpdatePost.php

<?php
namespace [Namespace]\[Modulename]\Controller\Cart;

use Magento\Framework\App\Action;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Cart as CustomerCart;
use Symfony\Component\Config\Definition\Exception\Exception;

class UpdatePost extends \Magento\Checkout\Controller\Cart\UpdatePost{


    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $_productFactory;

    /**
     * @var \Magento\Catalog\Model\CategoryFactory
     */
    protected $categoryFactory;


    /**
     * @var \Magento\Quote\Model\Quote\Item
     */
    protected $_quoteItem;


    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
        CustomerCart $cart,
        ProductRepositoryInterface $productRepository,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Quote\Model\Quote\Item $item
    ) {
        parent::__construct(
            $context,
            $scopeConfig,
            $checkoutSession,
            $storeManager,
            $formKeyValidator,
            $cart,
            $productRepository
        );
        $this->_productFactory = $productFactory;
        $this->productRepository = $productRepository;
        $this->categoryFactory = $categoryFactory;
        $this->_quoteItem = $item;
    }


    public function execute(){

        try{

            parent::execute();

            $grandTotal = $this->cart->getQuote()->getGrandTotal();
            $isFreeSample = false;
            $isFreeGift = false;

            if($grandTotal == 0 || $grandTotal < 1500){

                $categoryId = $this->getFreeSampleCategoryId();
                $free_sample_productCollection = $this->getProductCollection($categoryId);

                //$cartItems = $this->cart->getItems();
                $cartItems = $this->_checkoutSession->getQuote()->getAllVisibleItems();

                foreach($free_sample_productCollection as $_product){

                    foreach ($cartItems as $item) {

                        $_productId = (int) $item->getProductId();
                        if($_productId == $_product->getEntityId()){
                            $this->cart->removeItem($item->getItemId())->save();
                            $isFreeSample = true;
                        }

                    }

                }

                if($isFreeSample){
                    $free_sample_text = "Successfully removed Free Sample Product";
                    $message = __($free_sample_text,'Free Sample');
                    $this->messageManager->addSuccessMessage($message);
                    $isFreeSample = false;
                }

            }


        }catch (Exception $e){
            $this->messageManager->addError(__('We can\'t update the item.'));
            $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
            parent::execute();
        }
        return $this->_goBack();

    }

}

enter image description here

and I tried [Namespace]/[ModuelName]/etc/frontend/sections.xml also no luck

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="checkout/cart/updatePost">
        <section name="cart"/>
    </action>
</config>

Any help/suggestions?

13
  • Can you put more detail? Do you try section.xml for update minicart functionality?
    – Sohel Rana
    Jan 6 '17 at 6:52
  • @SohelRana see my thread is updated.
    – Bojjaiah
    Jan 6 '17 at 7:00
  • @SohelRana Any Help?
    – Bojjaiah
    Jan 6 '17 at 7:31
  • Can you delete 'sections.xml' and clear cache and try?
    – Sohel Rana
    Jan 6 '17 at 7:39
  • @SohelRana tried. no luck
    – Bojjaiah
    Jan 6 '17 at 7:52

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.