Magento – Disable cart sharing between different Stores of website in Magento 2

magento2.2.2quotesessionshopping-cart

My website is with Three different Stores and I'd like to disable the cart sharing feature between stores. So when I switch from one store to the other store, the product in my cart shouldn't be valid or not display to another store.

Product added to the specific store should be displayed on the specific store only.

Best Answer

I'm not sure if this works, it's just an idea: The quote has the functionality to specify the stores which are sharing the quote. By default this are all stores of a website. Anyway if it is possible to set a list of spefic stores before loading the quote, I think it is possible to restrict the quote to a subset of all stores of a website, in your case just to the actual store.

One idea could be to set the store restriction in a plugin before the get method in Magento\Quote\Model\QuoteRepository, which should always be called when a quote is loaded.

Again, it's just an idea which might not work and the code is not tested and just to demonstrate my thoughts.

class MyPlugin{

    protected $storeManager;

    public function __construct( 
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ){
        $this->storeManager = $storeManager;
    }

    public function beforeGet(
        \Magento\Quote\Model\QuoteRepository $quoteRepository, 
         $cartId, 
         array $sharedStoreIds
    ){
        return [$cartId, [$this->storeManager->getStore()->getId()]];
    }
}

Looking forward to hearing what you think about this approach.