1

I have a magento 2 website where I want to share cart between websites (for logged in users and guest users). For ex. guest user added product1 to the cart from website1 and now user switches to website2 then user would be able to see the same product to cart already.

magento is only providing cart sharing between stores not websites. Also I've tried couple of the multiple store cart sharing settings but no luck till now. However, I would love to hear solution for multiple websites.

2

To share cart among websites you have to do two things.

  1. Make Magento 2 read cart quote id from a single session key.

    class Session extends \Magento\Checkout\Model\Session
    {
        /**
         * @return string
         */
        protected function _getQuoteIdKey()
        {
            return 'quote_id_1';
        }
    }
    
  2. Make Magento consider all stores when loading quote

    class QuoteRepository extends \Magento\Quote\Model\QuoteRepository
    {
        /**
         * Load quote with different methods
         * Will consider all stores valid, quote can be shared among all stores.
         *
         * @param string $loadMethod
         * @param string $loadField
         * @param int $identifier
         * @param int[] $sharedStoreIds
         * @throws NoSuchEntityException
         * @return Quote
         */
        protected function loadQuote($loadMethod, $loadField, $identifier, array $sharedStoreIds = [])
        {
            $sharedStoreIds = $this->getStoreIds();
    
            /** @var Quote $quote */
            $quote = $this->quoteFactory->create();
            if ($sharedStoreIds) {
                $quote->setSharedStoreIds($sharedStoreIds);
            }
            $quote->setStoreId($this->storeManager->getStore()->getId())->$loadMethod($identifier);
            $quote->setStoreId($this->storeManager->getStore()->getId())->load($identifier);
            if (!$quote->getId()) {
                throw NoSuchEntityException::singleField($loadField, $identifier);
            }
            return $quote;
        }
    
    
        /**
         * @return int[]
         */
        protected function getStoreIds()
        {
            $storeIds = [];
            $stores = $this->storeManager->getStores();
    
            foreach ($stores as $storeId => $store) {
                $storeIds[] = $storeId;
            }
    
            return $storeIds;
        }
    }
    
  • Both way aren't working :( – Jimit Bhavsar Apr 28 '20 at 6:07
  • Impressive answer. I only needed to implement the first fix, as I need the cart to be shared between websites but I also want the quote to contain the store ID that the customer placed the order on. It works perfectly. – Geat Jun 25 '20 at 23:46
0

Please check the following link:

Share cart in magento 2 multistore

  • Hello Sukumar, the link you've given is for multiple store, not multiple website. – Virang Jethva May 18 '18 at 13:49
  • If it is working for multiple store then it should work for multiple website as well. Please try the same. – Sukumar Gorai May 18 '18 at 13:53
  • Hello Sukumar, Hope you are aware about that the magento is only providing cart sharing between stores not websites. Also I've tried couple of the multiple store cart sharing settings but no luck till now. However, I would love to hear solution for multiple websites. – Virang Jethva May 18 '18 at 14:15
  • 1
    Hello Sukumar, Would you please remove duplicate question line from your answer? As I've described already that how my question is different? – Virang Jethva May 22 '18 at 13:16

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.