Skip to content

Commit ccb3383

Browse files
committed
ACP2E-2062: Product Compare not honoring scope in Browser
1 parent 0a834cc commit ccb3383

File tree

4 files changed

+119
-24
lines changed

4 files changed

+119
-24
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ public function getItemCollection()
298298

299299
/* update compare items count */
300300
$count = count($this->_itemCollection);
301-
$counts = $this->_catalogSession->getCatalogCompareItemsCountPerWebsite() ?: [];
302301
$counts[$this->_storeManager->getWebsite()->getId()] = $count;
303302
$this->_catalogSession->setCatalogCompareItemsCountPerWebsite($counts);
304303
$this->_catalogSession->setCatalogCompareItemsCount($count); //deprecated
@@ -331,7 +330,6 @@ public function calculate($logout = false)
331330
->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds());
332331

333332
$count = $collection->getSize();
334-
$counts = $this->_catalogSession->getCatalogCompareItemsCountPerWebsite() ?: [];
335333
$counts[$this->_storeManager->getWebsite()->getId()] = $count;
336334
$this->_catalogSession->setCatalogCompareItemsCountPerWebsite($counts);
337335
$this->_catalogSession->setCatalogCompareItemsCount($count); //deprecated
@@ -349,6 +347,7 @@ public function getItemCount()
349347
$counts = $this->_catalogSession->getCatalogCompareItemsCountPerWebsite() ?: [];
350348
if (!isset($counts[$this->_storeManager->getWebsite()->getId()])) {
351349
$this->calculate();
350+
$counts = $this->_catalogSession->getCatalogCompareItemsCountPerWebsite() ?: [];
352351
}
353352

354353
return $counts[$this->_storeManager->getWebsite()->getId()] ?? 0;

app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,38 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product\Compare;
77

8+
use Magento\Customer\Model\Config\Share;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\Framework\App\ObjectManager;
11+
812
/**
913
* Catalog compare item resource model
1014
*
1115
* @author Magento Core Team <core@magentocommerce.com>
1216
*/
1317
class Item extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
1418
{
19+
/**
20+
* @var Share
21+
*/
22+
private $share;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
1529
/**
1630
* Initialize connection
1731
*
32+
* @param Share|null $share
33+
* @param StoreManagerInterface|null $storeManager
1834
* @return void
1935
*/
20-
protected function _construct()
36+
protected function _construct(?Share $share = null, ?StoreManagerInterface $storeManager = null)
2137
{
38+
$this->share = $share ?? ObjectManager::getInstance()->get(Share::class);
39+
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
2240
$this->_init('catalog_compare_item', 'catalog_compare_item_id');
2341
}
2442

@@ -41,8 +59,12 @@ public function loadByProduct(\Magento\Catalog\Model\Product\Compare\Item $objec
4159

4260
if ($object->getCustomerId()) {
4361
$select->where('customer_id = ?', (int)$object->getCustomerId());
62+
if (!$this->share->isGlobalScope()) {
63+
$select->where('store_id = ?', $this->storeManager->getStore()->getId());
64+
}
4465
} else {
4566
$select->where('visitor_id = ?', (int)$object->getVisitorId());
67+
$select->where('store_id = ?', $this->storeManager->getStore()->getId());
4668
}
4769

4870
if ($object->getListId()) {
@@ -236,10 +258,15 @@ public function clearItems($visitorId = null, $customerId = null)
236258
if ($customerId) {
237259
$customerId = (int)$customerId;
238260
$where[] = $this->getConnection()->quoteInto('customer_id = ?', $customerId);
261+
if (!$this->share->isGlobalScope()) {
262+
$where[] = $this->getConnection()
263+
->quoteInto('store_id = ?', $this->storeManager->getStore()->getId());
264+
}
239265
}
240266
if ($visitorId) {
241267
$visitorId = (int)$visitorId;
242268
$where[] = $this->getConnection()->quoteInto('visitor_id = ?', $visitorId);
269+
$where[] = $this->getConnection()->quoteInto('store_id = ?', $this->storeManager->getStore()->getId());
243270
}
244271
if (!$where) {
245272
return $this;

app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product\Compare\Item;
77

8+
use Magento\Customer\Model\Config\Share;
9+
use Magento\Framework\App\ObjectManager;
10+
811
/**
912
* Catalog Product Compare Items Resource Collection
1013
*
@@ -55,6 +58,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
5558
*/
5659
protected $_catalogProductCompareItem;
5760

61+
/**
62+
* @var Share
63+
*/
64+
private $share;
65+
5866
/**
5967
* Collection constructor.
6068
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
@@ -79,6 +87,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
7987
* @param \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem
8088
* @param \Magento\Catalog\Helper\Product\Compare $catalogProductCompare
8189
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
90+
* @param Share|null $share
8291
*
8392
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8493
*/
@@ -104,10 +113,12 @@ public function __construct(
104113
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
105114
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem,
106115
\Magento\Catalog\Helper\Product\Compare $catalogProductCompare,
107-
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null
116+
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
117+
?Share $share = null,
108118
) {
109119
$this->_catalogProductCompareItem = $catalogProductCompareItem;
110120
$this->_catalogProductCompare = $catalogProductCompare;
121+
$this->share = $share ?? ObjectManager::getInstance()->get(Share::class);
111122
parent::__construct(
112123
$entityFactory,
113124
$logger,
@@ -236,11 +247,15 @@ public function getVisitorId()
236247
public function getConditionForJoin()
237248
{
238249
if ($this->getCustomerId()) {
239-
return ['customer_id' => $this->getCustomerId()];
250+
$conditions['customer_id'] = $this->getCustomerId();
251+
if (!$this->share->isGlobalScope()) {
252+
$conditions['store_id'] = $this->getStoreId();
253+
}
254+
return $conditions;
240255
}
241256

242257
if ($this->getVisitorId()) {
243-
return ['visitor_id' => $this->getVisitorId()];
258+
return ['visitor_id' => $this->getVisitorId(), 'store_id' => $this->getStoreId()];
244259
}
245260

246261
if ($this->getListId()) {

dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/CompareTest.php

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,33 @@
66

77
namespace Magento\Catalog\Helper\Product;
88

9+
use Magento\Catalog\Helper\Data;
10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
11+
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
12+
use Magento\Store\Test\Fixture\Store as StoreFixture;
13+
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
14+
use Magento\TestFramework\Fixture\Config;
15+
use Magento\TestFramework\Fixture\DataFixture;
16+
use Magento\TestFramework\Fixture\DataFixtureStorage;
17+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
use Magento\Customer\Model\Visitor;
20+
921
class CompareTest extends \PHPUnit\Framework\TestCase
1022
{
1123
/**
1224
* @var \Magento\Catalog\Helper\Product\Compare
1325
*/
1426
protected $_helper;
1527

28+
/** @var StoreManagerInterface */
29+
private $storeManager;
30+
31+
/**
32+
* @var DataFixtureStorage
33+
*/
34+
private $fixtures;
35+
1636
/**
1737
* @var \Magento\Framework\ObjectManagerInterface
1838
*/
@@ -22,6 +42,8 @@ protected function setUp(): void
2242
{
2343
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
2444
$this->_helper = $this->_objectManager->get(\Magento\Catalog\Helper\Product\Compare::class);
45+
$this->fixtures = $this->_objectManager->get(DataFixtureStorageManager::class)->getStorage();
46+
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
2547
}
2648

2749
public function testGetListUrl()
@@ -73,25 +95,22 @@ public function testGetClearListUrl()
7395
);
7496
}
7597

76-
/**
77-
* @see testGetListUrl() for coverage of customer case
78-
*/
79-
public function testGetItemCollection()
80-
{
81-
$this->assertInstanceOf(
82-
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection::class,
83-
$this->_helper->getItemCollection()
84-
);
85-
}
86-
8798
/**
8899
* calculate()
89100
* getItemCount()
90101
* hasItems()
91102
*
92-
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
93103
* @magentoDbIsolation disabled
94104
*/
105+
#[
106+
Config(Data::XML_PATH_PRICE_SCOPE, Data::PRICE_SCOPE_WEBSITE),
107+
DataFixture(WebsiteFixture::class, as: 'website2'),
108+
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'store_group2'),
109+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store2'),
110+
DataFixture(ProductFixture::class, ['website_ids' => [1]], as: 'product1'),
111+
DataFixture(ProductFixture::class, ['website_ids' => [1, '$website2.id$']], as: 'product2'),
112+
DataFixture(ProductFixture::class, ['website_ids' => ['$website2.id$']], as: 'product3'),
113+
]
95114
public function testCalculate()
96115
{
97116
/** @var \Magento\Catalog\Model\Session $session */
@@ -101,18 +120,53 @@ public function testCalculate()
101120
$this->assertFalse($this->_helper->hasItems());
102121
$this->assertEquals(0, $session->getCatalogCompareItemsCount());
103122

104-
$this->_populateCompareList();
123+
$visitor = $this->_objectManager->get(Visitor::class);
124+
$visitor->setVisitorId(1);
125+
$this->_populateCompareList('product1');
126+
$this->_populateCompareList('product2');
105127
$this->_helper->calculate();
106128
$this->assertEquals(2, $session->getCatalogCompareItemsCount());
107129
$this->assertTrue($this->_helper->hasItems());
108130

131+
$secondStore = $this->fixtures->get('store2')->getCode();
132+
$this->storeManager->setCurrentStore($secondStore);
133+
$this->_helper->calculate();
134+
$this->assertEquals(0, $session->getCatalogCompareItemsCount());
135+
$this->_populateCompareList('product3');
136+
$this->_helper->calculate();
137+
$this->assertEquals(1, $session->getCatalogCompareItemsCount());
138+
$this->assertTrue($this->_helper->hasItems());
139+
$this->_populateCompareList('product2');
140+
$this->_helper->calculate();
141+
$this->assertEquals(2, $session->getCatalogCompareItemsCount());
142+
$this->assertTrue($this->_helper->hasItems());
143+
$compareItems = $this->_helper->getItemCollection();
144+
$compareItems->clear();
145+
$session->unsCatalogCompareItemsCountPerWebsite();
146+
$this->assertFalse($this->_helper->hasItems());
147+
$this->assertEquals(0, $session->getCatalogCompareItemsCount());
148+
$this->storeManager->setCurrentStore(1);
149+
$this->_helper->calculate();
150+
$this->assertEquals(2, $session->getCatalogCompareItemsCount());
151+
$this->assertTrue($this->_helper->hasItems());
109152
$session->unsCatalogCompareItemsCount();
110153
} catch (\Exception $e) {
111154
$session->unsCatalogCompareItemsCount();
112155
throw $e;
113156
}
114157
}
115158

159+
/**
160+
* @see testGetListUrl() for coverage of customer case
161+
*/
162+
public function testGetItemCollection()
163+
{
164+
$this->assertInstanceOf(
165+
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection::class,
166+
$this->_helper->getItemCollection()
167+
);
168+
}
169+
116170
public function testSetGetAllowUsedFlat()
117171
{
118172
$this->assertTrue($this->_helper->getAllowUsedFlat());
@@ -130,14 +184,14 @@ protected function _testGetProductUrl($method, $expectedFullAction)
130184

131185
/**
132186
* Add products from fixture to compare list
187+
*
188+
* @param string $sku
133189
*/
134-
protected function _populateCompareList()
190+
protected function _populateCompareList(string $sku)
135191
{
136-
$productRepository = $this->_objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
137-
$productOne = $productRepository->get('simple1');
138-
$productTwo = $productRepository->get('simple2');
192+
$product = $this->fixtures->get($sku);
139193
/** @var $compareList \Magento\Catalog\Model\Product\Compare\ListCompare */
140194
$compareList = $this->_objectManager->create(\Magento\Catalog\Model\Product\Compare\ListCompare::class);
141-
$compareList->addProduct($productOne)->addProduct($productTwo);
195+
$compareList->addProduct($product);
142196
}
143197
}

0 commit comments

Comments
 (0)