Menu Close
    Searching for an experienced eCommerce & Marketplace Development Company ?

    Multi Website guest cart Share Magento 2

    As We know in magento 2 , multi website is supported . We can run more than one website with different domains on a single magento instance.

    Magento does not provide default feature to share guest cart in magento 2.In this block I am going to explain how we can achieve guest cart share functionality.

    Challenge

    Main challenge in implementing this functionality was to share data between 2 different website as domain can be also different.

    Solution

    In magento when a product is added to cart, a quote is generated which has all the data regarding the current cart. Generally when we use multi website quote is generated website wise.

    For sharing the guest cart, we need to create a custom table which will save the data quote id with the remote address of the user from which product is added on the website A. Now on adding the product from Website A data will be saved on the custom table. When user will visit the Website B we need to apply check that any entry is avilable from same remote address and if quote is still in active state we need to create a quote programatically with the same items.
    This will create the same quote with the same items in website B also.


    Now for sharing the cart we need to create a preference for the loadQuote method of below class.
    “\Magento\Quote\Model\QuoteRepository”
    I have attached code for the reference-

    <?php
    namespace Webkul\MultiWebsiteCartManager\Plugin;
    use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
    use Magento\Framework\Api\Search\FilterGroup;
    use Magento\Framework\Api\SearchCriteria\CollectionProcessor;
    use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
    use Magento\Framework\Api\SearchCriteriaInterface;
    use Magento\Framework\App\ObjectManager;
    use Magento\Framework\Exception\InputException;
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Quote\Api\CartRepositoryInterface;
    use Magento\Quote\Api\Data\CartInterface;use Magento\Quote\Api\Data\CartInterfaceFactory;
    use Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory;
    use Magento\Quote\Model\QuoteRepository\SaveHandler;
    use Magento\Quote\Model\QuoteRepository\LoadHandler;
    use Magento\Quote\Model\ResourceModel\Quote\Collection as QuoteCollection;
    use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory as QuoteCollectionFactory;
    use Magento\Store\Model\StoreManagerInterface;
    /** 
    * Quote repository. * 
    * @SuppressWarnings(PHPMD.CouplingBetweenObjects) 
    */
    class QuoteRepository extends \Magento\Quote\Model\QuoteRepository
    { 
    public function __construct( \Magento\Quote\Model\QuoteFactory $quoteFactory, 
    StoreManagerInterface $storeManager, 
    QuoteCollection $quoteCollection, 
    CartSearchResultsInterfaceFactory $searchResultsDataFactory,
     JoinProcessorInterface $extensionAttributesJoinProcessor, 
    CollectionProcessorInterface $collectionProcessor = null, 
    QuoteCollectionFactory $quoteCollectionFactory = null, 
    CartInterfaceFactory $cartFactory = null ) { 
    $this->quoteFactory = $quoteFactory; 
    $this->storeManager = $storeManager; 
    $this->searchResultsDataFactory = $searchResultsDataFactory; 
    $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
     $this->collectionProcessor = $collectionProcessor ?: ObjectManager::getInstance() ->get(CollectionProcessor::class); 
    $this->quoteCollectionFactory = $quoteCollectionFactory ?: ObjectManager::getInstance() ->get(QuoteCollectionFactory::class); 
    $this->cartFactory = $cartFactory ?: ObjectManager::getInstance()->get(CartInterfaceFactory::class); parent::__construct( $quoteFactory, $storeManager, $quoteCollection, $searchResultsDataFactory, $extensionAttributesJoinProcessor,
     $collectionProcessor, $quoteCollectionFactory, $cartFactory ); } 
    /** 
    * Load quote with different methods * 
    * @param string $loadMethod 
    * @param string $loadField 
    * @param int $identifier 
    * @param int[] $sharedStoreIds 
    * @throws NoSuchEntityException 
    * @return CartInterface 
    */ 
    protected function loadQuote($loadMethod, $loadField, $identifier, array $sharedStoreIds = [])
     { $stores=$this->storeManager->getStores();
     /** @var CartInterface $quote */ 
    $quote = $this->cartFactory->create(); 
    if ($sharedStoreIds && is_callable([$quote, 'setSharedStoreIds'])) { 
    $quote->setSharedStoreIds($sharedStoreIds); 
    }
     foreach ($stores as $storeId => $store) {
     $quote->setStoreId($storeId)->$loadMethod($identifier); }
     if (!$quote->getId()) { throw NoSuchEntityException::singleField($loadField, $identifier); } 
    return $quote; }}
    



    In this way we can share guest cart in Magento 2.
    Happy Coding!

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top