2

Magento 2 : is it possible to disable cart sharing option with multi store setup (Not with multi website setup.). Cart sharing means: cart should not be share with other store. ex: if i add a item in store A then on store B my cart should be blank until i don't add a new item on store B.

Inventory management is possible at store level but just want to check that if we can stop cart sharing functionality at store level. Any plugin or any suggestions are welcome.

Any hint will be appreciated.

1 Answer 1

0

You can use the event controller_action_postdispatch_stores_store_switch to observe the store change event.

<event name="controller_action_postdispatch_stores_store_switch">
    <observer name="empty_cart_controller_action_postdispatch_stores_store_switch" instance="VendorName\ModuleName\Observer\Cart\Empty"/>
</event>

There you can check the current quote items and check its store if and delete from quote if not matched

    // get current store
    $currentActiveStore = $this->storeManager->getStore(); // use this class for store manager \Magento\Store\Model\StoreManagerInterface

    // check current quote items
    /** @var \Magento\Quote\Model\Quote $quote */
    $quote = $this->_checkoutSession->getQuote(); // use this class for checkout session \Magento\Checkout\Model\Session
    $allQuoteItems = $quote->getAllItems();

    foreach ($allQuoteItems as $quoteItem) {
        if ($quoteItem->getDisableAddToCart() && !$quoteItem->isDeleted()) {
            if($quoteItem->getStoreId() != $currentActiveStore->getId()) { // match with current store id
                $quote->removeItem($quoteItem->getQuoteId());
                $quote->deleteItem($quoteItem);
                $this->messageManager->addNotice(__('The item "%1" has been removed from your cart.', $quoteItem->getName()));
            }
        }
    }

Also you may need to check another event for the case when a logged in customer existing quote is being merged with current quote -

<!-- validate current quote items and customer quote items -->
<event name="load_customer_quote_before">
    <observer name="check_store_load_customer_quote_before" instance="VendorName\ModuleName\Observer\Customer\Quote\CheckStore"/>
</event>

and remove the items from quote which does not apply.

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.