Skip to content

Commit 5d23fb2

Browse files
committed
ACP2E-2224: Regular Price does not show on PLP for Configurable Product
- improvements for updated PAT failures - removed special price cache mapping
1 parent 220eb76 commit 5d23fb2

File tree

6 files changed

+76
-130
lines changed

6 files changed

+76
-130
lines changed

app/code/Magento/Catalog/Pricing/Price/SpecialPriceBulkResolver.php

Lines changed: 44 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
namespace Magento\Catalog\Pricing\Price;
2020

2121
use Magento\Catalog\Api\Data\ProductInterface;
22-
use Magento\Catalog\Model\Category;
23-
use Magento\Catalog\Model\Product;
2422
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
2523
use Magento\Framework\App\ResourceConnection;
26-
use Magento\Framework\Cache\FrontendInterface;
2724
use Magento\Framework\EntityManager\MetadataPool;
25+
use Magento\Framework\Session\SessionManagerInterface;
2826

2927
class SpecialPriceBulkResolver implements SpecialPriceBulkResolverInterface
3028
{
@@ -39,31 +37,23 @@ class SpecialPriceBulkResolver implements SpecialPriceBulkResolverInterface
3937
private MetadataPool $metadataPool;
4038

4139
/**
42-
* @var FrontendInterface
40+
* @var SessionManagerInterface
4341
*/
44-
private FrontendInterface $cache;
45-
46-
/**
47-
* @var int
48-
*/
49-
private int $cacheLifeTime;
42+
private SessionManagerInterface $customerSession;
5043

5144
/**
5245
* @param ResourceConnection $resource
5346
* @param MetadataPool $metadataPool
54-
* @param FrontendInterface $cache
55-
* @param int $cacheLifeTime
47+
* @param SessionManagerInterface $customerSession
5648
*/
5749
public function __construct(
5850
ResourceConnection $resource,
5951
MetadataPool $metadataPool,
60-
FrontendInterface $cache,
61-
int $cacheLifeTime = self::DEFAULT_CACHE_LIFE_TIME
52+
SessionManagerInterface $customerSession
6253
) {
6354
$this->resource = $resource;
6455
$this->metadataPool = $metadataPool;
65-
$this->cache = $cache;
66-
$this->cacheLifeTime = $cacheLifeTime;
56+
$this->customerSession = $customerSession;
6757
}
6858

6959
/**
@@ -79,108 +69,48 @@ public function generateSpecialPriceMap(int $storeId, ?AbstractCollection $produ
7969
if (!$productCollection) {
8070
return [];
8171
}
82-
//$cacheKey = $this->getCacheKey($storeId, $productCollection);
83-
//$cachedData = $this->getCachedData($cacheKey);
84-
if (true || $cachedData === null) {
85-
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
86-
$connection = $this->resource->getConnection();
87-
$select = $connection->select()
88-
->from(
89-
['e' => $this->resource->getTableName('catalog_product_entity')]
90-
)
91-
->joinLeft(
92-
['link' => $this->resource->getTableName('catalog_product_super_link')],
93-
'link.parent_id = e.' . $metadata->getLinkField()
94-
)
95-
->joinLeft(
96-
['product_website' => $this->resource->getTableName('catalog_product_website')],
97-
'product_website.product_id = link.product_id'
98-
)
99-
->joinLeft(
100-
['price' => $this->resource->getTableName('catalog_product_index_price')],
101-
'price.entity_id = COALESCE(link.product_id, e.entity_id) AND price.website_id = ' . $storeId .
102-
' AND price.customer_group_id = 0'
103-
)
104-
->where('e.entity_id IN (' . implode(',', $productCollection->getAllIds()) . ')')
105-
->columns(
106-
[
107-
'link.product_id',
108-
'(price.final_price < price.price) AS hasSpecialPrice',
109-
'e.' . $metadata->getLinkField() . ' AS identifier',
110-
'e.entity_id'
111-
]
112-
);
113-
$data = $connection->fetchAll($select);
114-
$map = [];
115-
foreach ($data as $specialPriceInfo) {
116-
if (!isset($map[$specialPriceInfo['entity_id']])) {
117-
$map[$specialPriceInfo['entity_id']] = (bool) $specialPriceInfo['hasSpecialPrice'];
118-
} else {
119-
if ($specialPriceInfo['hasSpecialPrice'] > $map[$specialPriceInfo['entity_id']]) {
120-
$map[$specialPriceInfo['entity_id']] = true;
121-
}
122-
}
12372

73+
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
74+
$connection = $this->resource->getConnection();
75+
$select = $connection->select()
76+
->from(
77+
['e' => $this->resource->getTableName('catalog_product_entity')]
78+
)
79+
->joinLeft(
80+
['link' => $this->resource->getTableName('catalog_product_super_link')],
81+
'link.parent_id = e.' . $metadata->getLinkField()
82+
)
83+
->joinLeft(
84+
['product_website' => $this->resource->getTableName('catalog_product_website')],
85+
'product_website.product_id = link.product_id'
86+
)
87+
->joinLeft(
88+
['price' => $this->resource->getTableName('catalog_product_index_price')],
89+
'price.entity_id = COALESCE(link.product_id, e.entity_id) AND price.website_id = ' . $storeId .
90+
' AND price.customer_group_id = ' . $this->customerSession->getCustomerGroupId()
91+
)
92+
->where('e.entity_id IN (' . implode(',', $productCollection->getAllIds()) . ')')
93+
->columns(
94+
[
95+
'link.product_id',
96+
'(price.final_price < price.price) AS hasSpecialPrice',
97+
'e.' . $metadata->getLinkField() . ' AS identifier',
98+
'e.entity_id'
99+
]
100+
);
101+
$data = $connection->fetchAll($select);
102+
$map = [];
103+
foreach ($data as $specialPriceInfo) {
104+
if (!isset($map[$specialPriceInfo['entity_id']])) {
105+
$map[$specialPriceInfo['entity_id']] = (bool) $specialPriceInfo['hasSpecialPrice'];
106+
} else {
107+
if ($specialPriceInfo['hasSpecialPrice'] > $map[$specialPriceInfo['entity_id']]) {
108+
$map[$specialPriceInfo['entity_id']] = true;
109+
}
124110
}
125-
//$this->saveCachedData($cacheKey, $map, array_column($data, 'identifier'));
126-
127-
return $map;
128-
}
129-
130-
return $cachedData;
131-
}
132-
133-
/**
134-
* Generate cache key
135-
*
136-
* @param int $storeId
137-
* @param AbstractCollection $productCollection
138-
* @return string
139-
*/
140-
private function getCacheKey(int $storeId, AbstractCollection $productCollection): string
141-
{
142-
$keyParts = $productCollection->getAllIds();
143-
$keyParts[] = 'store_id_' . $storeId;
144111

145-
return hash('sha256', implode('_', $keyParts));
146-
}
147-
148-
/**
149-
* Retrieve potential cached data
150-
*
151-
* @param string $cacheKey
152-
* @return array|null
153-
*/
154-
private function getCachedData(string $cacheKey): ?array
155-
{
156-
$data = $this->cache->load($cacheKey);
157-
if (!$data) {
158-
return null;
159-
}
160-
161-
return json_decode($data, true);
162-
}
163-
164-
/**
165-
* Store data in cache
166-
*
167-
* @param string $cacheKey
168-
* @param array $data
169-
* @param array $productTags
170-
* @return bool
171-
*/
172-
private function saveCachedData(string $cacheKey, array $data, array $productTags): bool
173-
{
174-
$tags = [
175-
Category::CACHE_TAG,
176-
Product::CACHE_TAG,
177-
'price'
178-
];
179-
$productTags = array_unique($productTags);
180-
foreach ($productTags as $tag) {
181-
$tags[] = Product::CACHE_TAG . '_' . $tag;
182112
}
183113

184-
return $this->cache->save(json_encode($data), $cacheKey, $tags, $this->cacheLifeTime);
114+
return $map;
185115
}
186116
}

app/code/Magento/Catalog/Test/Unit/Block/Product/ListProductTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public function testSetIsProductListFlagOnGetProductPrice()
266266
->method('getBlock')
267267
->with('product.price.render.default')
268268
->willReturn($this->renderer);
269-
269+
$this->block->setCollection($this->prodCollectionMock);
270270
$this->block->getProductPrice($this->productMock);
271271
}
272272
}

app/code/Magento/Catalog/Test/Unit/Pricing/Price/SpecialPriceBulkResolverTest.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
use Magento\Catalog\Pricing\Price\SpecialPriceBulkResolver;
2424
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
2525
use Magento\Framework\App\ResourceConnection;
26-
use Magento\Framework\Cache\FrontendInterface;
2726
use Magento\Framework\DB\Adapter\AdapterInterface;
2827
use Magento\Framework\EntityManager\EntityMetadataInterface;
2928
use Magento\Framework\EntityManager\MetadataPool;
29+
use Magento\Framework\Session\SessionManagerInterface;
3030
use PHPUnit\Framework\MockObject\MockObject;
3131
use PHPUnit\Framework\TestCase;
3232

@@ -43,14 +43,14 @@ class SpecialPriceBulkResolverTest extends TestCase
4343
private MetadataPool $metadataPool;
4444

4545
/**
46-
* @var FrontendInterface|MockObject
46+
* @var SpecialPriceBulkResolver|MockObject
4747
*/
48-
private FrontendInterface $cache;
48+
private SpecialPriceBulkResolver $specialPriceBulkResolver;
4949

5050
/**
51-
* @var SpecialPriceBulkResolver|MockObject
51+
* @var SessionManagerInterface
5252
*/
53-
private SpecialPriceBulkResolver $specialPriceBulkResolver;
53+
private SessionManagerInterface $customerSession;
5454

5555
/**
5656
* @return void
@@ -59,12 +59,15 @@ protected function setUp(): void
5959
{
6060
$this->resource = $this->createMock(ResourceConnection::class);
6161
$this->metadataPool = $this->createMock(MetadataPool::class);
62-
$this->cache = $this->getMockForAbstractClass(FrontendInterface::class);
62+
$this->customerSession = $this->getMockBuilder(SessionManagerInterface::class)
63+
->disableOriginalConstructor()
64+
->addMethods(['getCustomerGroupId'])
65+
->getMockForAbstractClass();
6366

6467
$this->specialPriceBulkResolver = new SpecialPriceBulkResolver(
6568
$this->resource,
6669
$this->metadataPool,
67-
$this->cache
70+
$this->customerSession
6871
);
6972
}
7073

@@ -85,17 +88,15 @@ public function testGenerateSpecialPriceMapCollection(): void
8588
{
8689
$product = $this->createMock(Product::class);
8790

91+
$this->customerSession->expects($this->once())->method('getCustomerGroupId')->willReturn(1);
8892
$collection = $this->getMockBuilder(AbstractCollection::class)
8993
->disableOriginalConstructor()
9094
->onlyMethods(['getAllIds', 'getIterator'])
9195
->getMockForAbstractClass();
92-
$collection->expects($this->exactly(2))->method('getAllIds')->willReturn([1]);
96+
$collection->expects($this->once())->method('getAllIds')->willReturn([1]);
9397
$collection->expects($this->any())->method('getIterator')
9498
->willReturn(new \ArrayIterator([$product]));
9599

96-
$this->cache->expects($this->once())->method('load')->willReturn(null);
97-
$this->cache->expects($this->once())->method('save')->willReturn(true);
98-
99100
$metadata = $this->getMockForAbstractClass(EntityMetadataInterface::class);
100101
$metadata->expects($this->exactly(2))->method('getLinkField')->willReturn('row_id');
101102
$this->metadataPool->expects($this->once())

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@
13481348
type="Magento\Catalog\Pricing\Price\SpecialPriceBulkResolver" />
13491349
<type name="Magento\Catalog\Pricing\Price\SpecialPriceBulkResolverInterface">
13501350
<arguments>
1351-
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Collection</argument>
1351+
<argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session</argument>
13521352
</arguments>
13531353
</type>
13541354
</config>

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Block/Product/View/Type/ConfigurableViewOnCategoryPageTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ protected function setUp(): void
6161
parent::setUp();
6262

6363
$this->objectManager = Bootstrap::getObjectManager();
64-
/** @var \Magento\Framework\App\Cache\StateInterface $cacheState */
65-
$cacheState = $this->objectManager->get(\Magento\Framework\App\Cache\StateInterface::class);
66-
$cacheState->setEnabled(\Magento\Framework\App\Cache\Type\Collection::TYPE_IDENTIFIER, true);
6764
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
6865
$this->productRepository->cleanCache();
6966
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);

dev/tests/integration/testsuite/Magento/Weee/Block/Product/View/Attribute/FixedProductTaxAttributeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
1212
use Magento\Catalog\Block\Product\ListProduct;
13+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1314
use Magento\Catalog\Pricing\Render as CatalogPricingRender;
1415
use Magento\Customer\Api\CustomerRepositoryInterface;
1516
use Magento\Customer\Model\Session;
17+
use Magento\Framework\Exception\LocalizedException;
1618
use Magento\Framework\ObjectManagerInterface;
1719
use Magento\Framework\Pricing\Render;
1820
use Magento\Framework\Pricing\Render\RendererPool;
@@ -67,6 +69,11 @@ class FixedProductTaxAttributeTest extends TestCase
6769
/** @var int */
6870
private $baseWebsiteId;
6971

72+
/**
73+
* @var Collection
74+
*/
75+
private $productCollection;
76+
7077
/**
7178
* @inheritdoc
7279
*/
@@ -93,6 +100,7 @@ protected function setUp(): void
93100
'state' => '',
94101
]
95102
];
103+
$this->productCollection = $this->objectManager->create(Collection::class);
96104
}
97105

98106
/**
@@ -112,11 +120,14 @@ protected function tearDown(): void
112120
* @magentoConfigFixture default_store tax/weee/display_list 0
113121
*
114122
* @return void
123+
* @throws LocalizedException
115124
*/
116125
public function testFPTCategoryPageIncludingFPTOnly(): void
117126
{
118127
$this->prepareLayoutCategoryPage();
119128
$product = $this->updateProduct('simple2', $this->textTaxData);
129+
$this->productCollection->addItem($product);
130+
$this->productListBlock->setCollection($this->productCollection);
120131
$productPrice = $this->productListBlock->getProductPrice($product);
121132
$this->assertEquals('$15.00', preg_replace('/\s+/', '', strip_tags($productPrice)));
122133
}
@@ -126,11 +137,14 @@ public function testFPTCategoryPageIncludingFPTOnly(): void
126137
* @magentoConfigFixture default_store tax/weee/display_list 1
127138
*
128139
* @return void
140+
* @throws LocalizedException
129141
*/
130142
public function testFPTCategoryPageIncludingFPTAndDescription(): void
131143
{
132144
$this->prepareLayoutCategoryPage();
133145
$product = $this->updateProduct('simple2', $this->textTaxData);
146+
$this->productCollection->addItem($product);
147+
$this->productListBlock->setCollection($this->productCollection);
134148
$productPrice = $this->productListBlock->getProductPrice($product);
135149
$this->assertStringContainsString('data-label="fixed&#x20;product&#x20;tax"', $productPrice);
136150
$this->assertEquals('$15.00$5.00', preg_replace('/\s+/', '', strip_tags($productPrice)));
@@ -146,6 +160,8 @@ public function testFPTCategoryPageExcludingFPTIncludingDescriptionAndPrice(): v
146160
{
147161
$this->prepareLayoutCategoryPage();
148162
$product = $this->updateProduct('simple2', $this->textTaxData);
163+
$this->productCollection->addItem($product);
164+
$this->productListBlock->setCollection($this->productCollection);
149165
$productPrice = $this->productListBlock->getProductPrice($product);
150166
$this->assertStringContainsString('data-label="fixed&#x20;product&#x20;tax"', $productPrice);
151167
$this->assertEquals('$10.00$5.00$15.00', preg_replace('/\s+/', '', strip_tags($productPrice)));
@@ -161,6 +177,8 @@ public function testFPTCategoryPageExcludingFPT(): void
161177
{
162178
$this->prepareLayoutCategoryPage();
163179
$product = $this->updateProduct('simple2', $this->textTaxData);
180+
$this->productCollection->addItem($product);
181+
$this->productListBlock->setCollection($this->productCollection);
164182
$productPrice = $this->productListBlock->getProductPrice($product);
165183
$this->assertEquals('$10.00', preg_replace('/\s+/', '', strip_tags($productPrice)));
166184
}

0 commit comments

Comments
 (0)