Skip to content

Commit 206c58c

Browse files
Chhandak.BaruaChhandak.Barua
authored andcommitted
ACP2E-1007 Compare list redirect to old store
1 parent 2e45de7 commit 206c58c

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

app/code/Magento/Catalog/CustomerData/CompareProducts.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\App\Config\ScopeConfigInterface;
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Store\Model\StoreManagerInterface;
1213

1314
/**
1415
* Catalog Product Compare Widget
@@ -37,22 +38,30 @@ class CompareProducts implements SectionSourceInterface
3738
*/
3839
private $scopeConfig;
3940

41+
/**
42+
* @var StoreManagerInterface
43+
*/
44+
private $storeManager;
45+
4046
/**
4147
* @param \Magento\Catalog\Helper\Product\Compare $helper
4248
* @param \Magento\Catalog\Model\Product\Url $productUrl
4349
* @param \Magento\Catalog\Helper\Output $outputHelper
4450
* @param ScopeConfigInterface|null $scopeConfig
51+
* @param StoreManagerInterface|null $storeManager
4552
*/
4653
public function __construct(
4754
\Magento\Catalog\Helper\Product\Compare $helper,
4855
\Magento\Catalog\Model\Product\Url $productUrl,
4956
\Magento\Catalog\Helper\Output $outputHelper,
50-
?ScopeConfigInterface $scopeConfig = null
57+
?ScopeConfigInterface $scopeConfig = null,
58+
?StoreManagerInterface $storeManager = null
5159
) {
5260
$this->helper = $helper;
5361
$this->productUrl = $productUrl;
5462
$this->outputHelper = $outputHelper;
5563
$this->scopeConfig = $scopeConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
64+
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
5665
}
5766

5867
/**
@@ -66,6 +75,7 @@ public function getSectionData()
6675
'countCaption' => $count == 1 ? __('1 item') : __('%1 items', $count),
6776
'listUrl' => $this->helper->getListUrl(),
6877
'items' => $count ? $this->getItems() : [],
78+
'websiteId' => $this->storeManager->getWebsite()->getId()
6979
];
7080
}
7181

app/code/Magento/Catalog/Helper/Product/Compare.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,12 @@ public function getItemCollection()
297297
$this->_itemCollection->addAttributeToSelect('name')->addUrlRewrite()->load();
298298

299299
/* update compare items count */
300-
$this->_catalogSession->setCatalogCompareItemsCount(count($this->_itemCollection));
300+
$this->_catalogSession->setCatalogCompareWebsiteId($this->_storeManager->getWebsite()->getId());
301+
$count = count($this->_itemCollection);
302+
$counts = $this->_catalogSession->getCatalogCompareItemsCountPerWebsite() ?: [];
303+
$counts[$this->_storeManager->getWebsite()->getId()] = $count;
304+
$this->_catalogSession->setCatalogCompareItemsCountPerWebsite($counts);
305+
$this->_catalogSession->setCatalogCompareItemsCount($count); //deprecated
301306
}
302307

303308
return $this->_itemCollection;
@@ -327,7 +332,10 @@ public function calculate($logout = false)
327332
->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds());
328333

329334
$count = $collection->getSize();
330-
$this->_catalogSession->setCatalogCompareItemsCount($count);
335+
$counts = $this->_catalogSession->getCatalogCompareItemsCountPerWebsite() ?: [];
336+
$counts[$this->_storeManager->getWebsite()->getId()] = $count;
337+
$this->_catalogSession->setCatalogCompareItemsCountPerWebsite($counts);
338+
$this->_catalogSession->setCatalogCompareItemsCount($count); //deprecated
331339

332340
return $this;
333341
}
@@ -339,11 +347,12 @@ public function calculate($logout = false)
339347
*/
340348
public function getItemCount()
341349
{
342-
if (!$this->_catalogSession->hasCatalogCompareItemsCount()) {
350+
$counts = $this->_catalogSession->getCatalogCompareItemsCountPerWebsite() ?: [];
351+
if (!isset($counts[$this->_storeManager->getWebsite()->getId()])) {
343352
$this->calculate();
344353
}
345354

346-
return $this->_catalogSession->getCatalogCompareItemsCount();
355+
return $counts[$this->_storeManager->getWebsite()->getId()] ?? 0;
347356
}
348357

349358
/**

app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ define([
3131
initialize: function () {
3232
this._super();
3333
this.compareProducts = customerData.get('compare-products');
34-
34+
if (window.checkout
35+
&& window.checkout.websiteId
36+
&& window.checkout.websiteId !== this.compareProducts().websiteId
37+
// TODO this code is only needed if websites share same domain (store code in url)
38+
) {
39+
//set count to 0 to prevent "compared products" blocks and count to show with wrong count and items
40+
this.compareProducts().count = 0;
41+
customerData.reload(['compare-products'], false);
42+
}
3543
initSidebar();
3644
}
3745
});

0 commit comments

Comments
 (0)