{"id":300827,"date":"2021-08-23T10:27:27","date_gmt":"2021-08-23T10:27:27","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=300827"},"modified":"2021-08-23T10:27:29","modified_gmt":"2021-08-23T10:27:29","slug":"multi-website-guest-cart-share-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/","title":{"rendered":"Multi Website guest cart Share Magento 2"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p class=\"has-large-font-size\">Challenge <\/p>\n\n\n\n<p>Main challenge in implementing this functionality was to share data between 2 different website as domain can be also different.<\/p>\n\n\n\n<p class=\"has-large-font-size\">Solution<\/p>\n\n\n\n<p>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.  <\/p>\n\n\n\n<p>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.<br>This will create the same quote with the same items in website B also.<\/p>\n\n\n\n<p><br>Now for sharing the cart we need to create a preference for the <strong>loadQuote<\/strong> method of below class.<br><strong>&#8220;\\Magento\\Quote\\Model\\QuoteRepository&#8221;<\/strong><br>I have attached code for the reference-<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nnamespace Webkul\\MultiWebsiteCartManager\\Plugin;\nuse Magento\\Framework\\Api\\ExtensionAttribute\\JoinProcessorInterface;\nuse Magento\\Framework\\Api\\Search\\FilterGroup;\nuse Magento\\Framework\\Api\\SearchCriteria\\CollectionProcessor;\nuse Magento\\Framework\\Api\\SearchCriteria\\CollectionProcessorInterface;\nuse Magento\\Framework\\Api\\SearchCriteriaInterface;\nuse Magento\\Framework\\App\\ObjectManager;\nuse Magento\\Framework\\Exception\\InputException;\nuse Magento\\Framework\\Exception\\NoSuchEntityException;\nuse Magento\\Quote\\Api\\CartRepositoryInterface;\nuse Magento\\Quote\\Api\\Data\\CartInterface;use Magento\\Quote\\Api\\Data\\CartInterfaceFactory;\nuse Magento\\Quote\\Api\\Data\\CartSearchResultsInterfaceFactory;\nuse Magento\\Quote\\Model\\QuoteRepository\\SaveHandler;\nuse Magento\\Quote\\Model\\QuoteRepository\\LoadHandler;\nuse Magento\\Quote\\Model\\ResourceModel\\Quote\\Collection as QuoteCollection;\nuse Magento\\Quote\\Model\\ResourceModel\\Quote\\CollectionFactory as QuoteCollectionFactory;\nuse Magento\\Store\\Model\\StoreManagerInterface;\n\/** \n* Quote repository. * \n* @SuppressWarnings(PHPMD.CouplingBetweenObjects) \n*\/\nclass QuoteRepository extends \\Magento\\Quote\\Model\\QuoteRepository\n{ \npublic function __construct( \\Magento\\Quote\\Model\\QuoteFactory $quoteFactory, \nStoreManagerInterface $storeManager, \nQuoteCollection $quoteCollection, \nCartSearchResultsInterfaceFactory $searchResultsDataFactory,\n JoinProcessorInterface $extensionAttributesJoinProcessor, \nCollectionProcessorInterface $collectionProcessor = null, \nQuoteCollectionFactory $quoteCollectionFactory = null, \nCartInterfaceFactory $cartFactory = null ) { \n$this-&gt;quoteFactory = $quoteFactory; \n$this-&gt;storeManager = $storeManager; \n$this-&gt;searchResultsDataFactory = $searchResultsDataFactory; \n$this-&gt;extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;\n $this-&gt;collectionProcessor = $collectionProcessor ?: ObjectManager::getInstance() -&gt;get(CollectionProcessor::class); \n$this-&gt;quoteCollectionFactory = $quoteCollectionFactory ?: ObjectManager::getInstance() -&gt;get(QuoteCollectionFactory::class); \n$this-&gt;cartFactory = $cartFactory ?: ObjectManager::getInstance()-&gt;get(CartInterfaceFactory::class); parent::__construct( $quoteFactory, $storeManager, $quoteCollection, $searchResultsDataFactory, $extensionAttributesJoinProcessor,\n $collectionProcessor, $quoteCollectionFactory, $cartFactory ); } \n\/** \n* Load quote with different methods * \n* @param string $loadMethod \n* @param string $loadField \n* @param int $identifier \n* @param int&#091;] $sharedStoreIds \n* @throws NoSuchEntityException \n* @return CartInterface \n*\/ \nprotected function loadQuote($loadMethod, $loadField, $identifier, array $sharedStoreIds = &#091;])\n { $stores=$this-&gt;storeManager-&gt;getStores();\n \/** @var CartInterface $quote *\/ \n$quote = $this-&gt;cartFactory-&gt;create(); \nif ($sharedStoreIds &amp;&amp; is_callable(&#091;$quote, &#039;setSharedStoreIds&#039;])) { \n$quote-&gt;setSharedStoreIds($sharedStoreIds); \n}\n foreach ($stores as $storeId =&gt; $store) {\n $quote-&gt;setStoreId($storeId)-&gt;$loadMethod($identifier); }\n if (!$quote-&gt;getId()) { throw NoSuchEntityException::singleField($loadField, $identifier); } \nreturn $quote; }}<\/pre>\n\n\n\n<p class=\"has-text-align-left\"><br><br>In this way we can share guest cart in Magento 2.<br>Happy Coding!<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. <a href=\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":363,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2070],"class_list":["post-300827","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-magento2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>multi-website-guest-cart-share-magento-2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"multi-website-guest-cart-share-magento-2\" \/>\n<meta property=\"og:description\" content=\"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. [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-23T10:27:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-23T10:27:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ankit Gupta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ankit Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/\"},\"author\":{\"name\":\"Ankit Gupta\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f80688803393714fc475e402958e1cf4\"},\"headline\":\"Multi Website guest cart Share Magento 2\",\"datePublished\":\"2021-08-23T10:27:27+00:00\",\"dateModified\":\"2021-08-23T10:27:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/\"},\"wordCount\":258,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/\",\"name\":\"multi-website-guest-cart-share-magento-2\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2021-08-23T10:27:27+00:00\",\"dateModified\":\"2021-08-23T10:27:29+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Multi Website guest cart Share Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f80688803393714fc475e402958e1cf4\",\"name\":\"Ankit Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9f0a3e7725ef351d66cceeb729b8b1e96cb1688bdc90ce596c6708c38a7a5b98?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9f0a3e7725ef351d66cceeb729b8b1e96cb1688bdc90ce596c6708c38a7a5b98?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ankit Gupta\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/ankit-gupta378\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"multi-website-guest-cart-share-magento-2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"multi-website-guest-cart-share-magento-2","og_description":"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. [...]","og_url":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-08-23T10:27:27+00:00","article_modified_time":"2021-08-23T10:27:29+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ankit Gupta","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ankit Gupta","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/"},"author":{"name":"Ankit Gupta","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f80688803393714fc475e402958e1cf4"},"headline":"Multi Website guest cart Share Magento 2","datePublished":"2021-08-23T10:27:27+00:00","dateModified":"2021-08-23T10:27:29+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/"},"wordCount":258,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/","url":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/","name":"multi-website-guest-cart-share-magento-2","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2021-08-23T10:27:27+00:00","dateModified":"2021-08-23T10:27:29+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/multi-website-guest-cart-share-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Multi Website guest cart Share Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f80688803393714fc475e402958e1cf4","name":"Ankit Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9f0a3e7725ef351d66cceeb729b8b1e96cb1688bdc90ce596c6708c38a7a5b98?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9f0a3e7725ef351d66cceeb729b8b1e96cb1688bdc90ce596c6708c38a7a5b98?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ankit Gupta"},"url":"https:\/\/webkul.com\/blog\/author\/ankit-gupta378\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/300827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/363"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=300827"}],"version-history":[{"count":4,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/300827\/revisions"}],"predecessor-version":[{"id":302344,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/300827\/revisions\/302344"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=300827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=300827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=300827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}