1

after customer login ,i am add filter on current customer quote, and remove items form quote which are not match. so item are delete successfully but front side in minicart cart item count are not updated .

use : "customer_login" event

  • Use section.xml file to unvalidate cart after login – Mohamed El Mrabet Nov 27 '17 at 12:11
3

You can do it using

Vendor/Module/etc/frontend/sections.xml

file.

<?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="customer/account/logout">
        <section name="cart"/>
    </action>
    <action name="customer/account/loginPost">
        <section name="cart"/>
    </action>
    <action name="customer/account/createPost">
        <section name="cart"/>
    </action>
    <action name="customer/ajax/login">
        <section name="cart"/>
    </action>
    <action name="checkout/cart/add">
        <section name="cart"/>
    </action>
    <action name="checkout/cart/delete">
        <section name="cart"/>
    </action>
    <action name="checkout/cart/updatePost">
        <section name="cart"/>
    </action>
    <action name="checkout/cart/updateItemOptions">
        <section name="cart"/>
    </action>
    <action name="checkout/cart/couponPost">
        <section name="cart"/>
    </action>
    <action name="checkout/cart/estimatePost">
        <section name="cart"/>
    </action>
    <action name="checkout/cart/estimateUpdatePost">
        <section name="cart"/>
    </action>
    <action name="checkout/onepage/saveOrder">
        <section name="cart"/>
        <section name="checkout-data"/>
        <section name="last-ordered-items"/>
    </action>
    <action name="checkout/sidebar/removeItem">
        <section name="cart"/>
    </action>
    <action name="checkout/sidebar/updateItemQty">
        <section name="cart"/>
    </action>
    <action name="rest/*/V1/carts/*/payment-information">
        <section name="cart"/>
        <section name="checkout-data"/>
        <section name="last-ordered-items"/>
    </action>
    <action name="rest/*/V1/guest-carts/*/payment-information">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>
    <action name="rest/*/V1/guest-carts/*/selected-payment-method">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>
    <action name="rest/*/V1/carts/*/selected-payment-method">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>
</config>

In every action your mini cart will refresh.

1

Solution : i am remove items form quote based on my custom filter after customer login.so i am used customer_login event.

File : Doyenhub\RestrictCustomer\etc\frontend\events.xml

    <event name="customer_login">
        <observer name="customer_login_observer" instance="Doyenhub\RestrictCustomer\Observer\CustomerLoginAfter" />
    </event>

And My Observer class is Doyenhub\RestrictCustomer\Observer\CustomerLoginAfter.php

class CustomerLoginAfter implements ObserverInterface {

        protected $quoteRepository;

        public function __construct(
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Quote\Model\Quote\Item $quoteItem
        ) {
            $this->_quoteItem = $quoteItem;
            $this->quoteRepository = $quoteRepository;
        }    

     public function execute(\Magento\Framework\Event\Observer $observer) {
                $customer = $observer->getEvent()->getCustomer();
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                //$quoteId = get quote id 
                $cartData = $objectManager->create('Magento\Quote\Model\QuoteRepository')->get($quoteId);
                $items = $cartData->getAllItems();
                 foreach ($items as $item) 
                 {
                  $quoteItem = $this->_quoteItem->load($item->getItemId());
                  $quoteItem->delete();//delete item
                 }
            //=======referesh quote=======
                 $quote = $this->quoteRepository->get($quoteId);
                 $quote->setCustomerId($customer->getId()); // Whatever you want to update
                 $this->quoteRepository->save($quote);
        }
}

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.