9

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.
2
  • I think you have one web -> one store -> multiple views ? Commented Feb 27, 2019 at 7:24
  • I have 1 website, 2 stores and 2 store views (one for each store).
    – amitshree
    Commented Feb 27, 2019 at 10:22

3 Answers 3

3
+50

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.

4
  • I will try and update if it works.
    – amitshree
    Commented Mar 5, 2019 at 6:34
  • Great Man !!! :) +1 Commented Mar 7, 2019 at 4:18
  • Tried it. Only items are hidden from minicart section but they are visible is other sections like cart, checkout.
    – amitshree
    Commented Mar 20, 2019 at 7:19
  • I tried the above solution. Working fine but not after login. Any guide? Commented Apr 22, 2022 at 9:59
1

Go to folder location vendor/magento/module-checkout/Model/ open file Session.php. Now search _getQuoteIdKey function in this file and replace website id with store id.

Replace this line

return 'quote_id_' . $this->_storeManager->getStore()->getWebsiteId() with this line
return 'quote_id_' . $this->_storeManager->getStore()->getStoreId();

now your all stores carts who belongs to same website works independently.

1
  • But this is not working for API calls. /V1/carts/mine returns the existing quote id Commented Aug 19, 2020 at 5:15
0

By default, Magento support to share cart between websites and to do so you need to ensure the following settings:

  1. Cookie Path & Cookie Domain

cookie setting

  1. Share Customer Account -> Global

share customer account

To disable cart sharing functionality, I suggest to change your cookie domain. E.g

store 1: https://mystore.com => cookie domain: .mystore.com

store 2: https://mystore2.com => cookie domain: .mystore2.com

And disable the share customer account functionality, otherwise you may need to find a way to regenerate the session ID between stores (SID), Magento store switcher automatically added SID for store, I recommend not use their store switcher but develop your owned store switcher to avoid SID appended.

Hope this help.

3
  • I have only one website and loading second store using website.com/store2 . Is is compulsory to create 2 websites?
    – amitshree
    Commented Mar 5, 2019 at 6:31
  • @amitshree If your store using domain such as website.com/store2, you should look into the cookie path instead of the cookie domain. Commented Mar 6, 2019 at 7:02
  • I have changed cookie path as /default and /retail for both stores view. Still I see cart is shared.
    – amitshree
    Commented Mar 20, 2019 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.