Skip to content

Commit 4f25bf7

Browse files
committed
ACP2E-1992: Configurable on sale products not visible in products carousel
- adjusted page-builder widget total count
1 parent ac38244 commit 4f25bf7

File tree

2 files changed

+53
-50
lines changed

2 files changed

+53
-50
lines changed

app/code/Magento/PageBuilder/Model/Catalog/ProductTotals.php

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class ProductTotals
6666
*/
6767
private ResourceConnection $resource;
6868

69-
private ?Collection $parentsCollection = null;
70-
7169
/**
7270
* @param CollectionFactory $productCollectionFactory
7371
* @param Builder $sqlBuilder
@@ -184,34 +182,32 @@ private function getProductCollection(string $conditions, bool $usePriceIndex =
184182
* Get parent products that don't have stand-alone properties (e.g. price or special price)
185183
*
186184
* @param Collection $collection
187-
* @return Collection
185+
* @return Collection|null
188186
* @throws \Exception
189187
*/
190-
private function getParentProductsCollection(Collection $collection): Collection
188+
private function getParentProductsCollection(Collection $collection): ?Collection
191189
{
192-
if (!$this->parentsCollection) {
193-
$ids = $collection->getAllIds();
194-
$linkField = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
195-
->getLinkField();
196-
$connection = $this->resource->getConnection();
197-
$productIds = $connection->fetchCol(
198-
$connection
199-
->select()
200-
->from(['e' => $collection->getTable('catalog_product_entity')], ['link_table.parent_id'])
201-
->joinInner(
202-
['link_table' => $collection->getTable('catalog_product_super_link')],
203-
'link_table.product_id = e.' . $linkField,
204-
[]
205-
)
206-
->where('link_table.product_id IN (?)', $ids)
207-
);
208-
209-
$parentProducts = $this->productCollectionFactory->create();
190+
$parentProducts = $this->productCollectionFactory->create();
191+
$linkField = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
192+
->getLinkField();
193+
$connection = $this->resource->getConnection();
194+
$productIds = $connection->fetchCol(
195+
$connection
196+
->select()
197+
->from(['e' => $collection->getTable('catalog_product_entity')], ['link_table.parent_id'])
198+
->joinInner(
199+
['link_table' => $collection->getTable('catalog_product_super_link')],
200+
'link_table.product_id = e.' . $linkField,
201+
[]
202+
)
203+
->where('link_table.product_id IN (?)', $collection->getAllIds())
204+
);
205+
if ($productIds) {
210206
$parentProducts->addIdFilter($productIds);
211-
$this->parentsCollection = $parentProducts;
207+
return $parentProducts;
212208
}
213209

214-
return $this->parentsCollection;
210+
return null;
215211
}
216212

217213
/**
@@ -226,11 +222,13 @@ private function getEnabledCount(string $conditions): int
226222
{
227223
$collection = $this->getProductCollection($conditions, true);
228224
$collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
225+
$count = $collection->getSize();
226+
if ($collection->getSize() && $parentProducts = $this->getParentProductsCollection($collection)) {
227+
$parentProducts->addAttributeToFilter('status', Status::STATUS_ENABLED);
228+
$count += $parentProducts->getSize();
229+
}
229230

230-
$parentProducts = $this->getParentProductsCollection($collection);
231-
$parentProducts->addAttributeToFilter('status', Status::STATUS_ENABLED);
232-
233-
return $collection->getSize() + $parentProducts->getSize();
231+
return $count;
234232
}
235233

236234
/**
@@ -244,11 +242,13 @@ private function getDisabledCount(string $conditions): int
244242
{
245243
$collection = $this->getProductCollection($conditions, false);
246244
$collection->addAttributeToFilter('status', Status::STATUS_DISABLED);
245+
$count = $collection->getSize();
246+
if ($count && $parentProducts = $this->getParentProductsCollection($collection)) {
247+
$parentProducts->addAttributeToFilter('status', Status::STATUS_DISABLED);
248+
$count += $parentProducts->getSize();
249+
}
247250

248-
$parentProducts = $this->getParentProductsCollection($collection);
249-
$parentProducts->addAttributeToFilter('status', Status::STATUS_DISABLED);
250-
251-
return $collection->getSize() + $parentProducts->getSize();
251+
return $count;
252252
}
253253

254254
/**
@@ -269,18 +269,20 @@ private function getNotVisibleCount(string $conditions): int
269269
Visibility::VISIBILITY_IN_SEARCH
270270
]
271271
);
272+
$count = $collection->getSize();
273+
if ($count && $parentProducts = $this->getParentProductsCollection($collection)) {
274+
$parentProducts->addAttributeToFilter('status', Status::STATUS_ENABLED);
275+
$parentProducts->addAttributeToFilter(
276+
'visibility',
277+
[
278+
Visibility::VISIBILITY_NOT_VISIBLE,
279+
Visibility::VISIBILITY_IN_SEARCH
280+
]
281+
);
282+
$count += $parentProducts->getSize();
283+
}
272284

273-
$parentProducts = $this->getParentProductsCollection($collection);
274-
$parentProducts->addAttributeToFilter('status', Status::STATUS_ENABLED);
275-
$parentProducts->addAttributeToFilter(
276-
'visibility',
277-
[
278-
Visibility::VISIBILITY_NOT_VISIBLE,
279-
Visibility::VISIBILITY_IN_SEARCH
280-
]
281-
);
282-
283-
return $collection->getSize() + $parentProducts->getSize();
285+
return $count;
284286
}
285287

286288
/**

app/code/Magento/PageBuilder/Test/Unit/Model/Catalog/ProductTotalsTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,19 @@ public function testGetProductTotals(): void
101101
$collection = $this->createMock(Collection::class);
102102
$collection->expects($this->exactly(3))->method('distinct');
103103
$collection->expects($this->any())->method('addAttributeToFilter');
104-
$collection->expects($this->once())->method('getAllIds');
104+
$collection->expects($this->exactly(3))->method('getAllIds');
105+
$collection->expects($this->any())->method('getSize')->willReturn(1);
105106
$select = $this->createMock(Select::class);
106107
$select->expects($this->any())->method('joinLeft')->willReturn($select);
107108

108109
$collection->expects($this->exactly(3))->method('getSelect')->willReturn($select);
109-
$this->productCollectionFactory->expects($this->exactly(4))
110+
$this->productCollectionFactory->expects($this->any())
110111
->method('create')
111112
->willReturn($collection);
112113

113114
$entityMeta = $this->createMock(EntityMetadataInterface::class);
114-
$entityMeta->expects($this->once())->method('getLinkField')->willReturn('row_id');
115-
$this->metadataPool->expects($this->exactly(1))
115+
$entityMeta->expects($this->exactly(3))->method('getLinkField')->willReturn('row_id');
116+
$this->metadataPool->expects($this->exactly(3))
116117
->method('getMetadata')
117118
->with(\Magento\Catalog\Api\Data\ProductInterface::class)
118119
->willReturn($entityMeta);
@@ -121,9 +122,9 @@ public function testGetProductTotals(): void
121122
$parentSelect->expects($this->any())->method('from')->willReturn($parentSelect);
122123
$parentSelect->expects($this->any())->method('joinInner')->willReturn($parentSelect);
123124
$db = $this->createMock(AdapterInterface::class);
124-
$db->expects($this->once())->method('select')->willReturn($parentSelect);
125-
$db->expects($this->once())->method('fetchCol')->willReturn([1]);
126-
$this->resource->expects($this->once())->method('getConnection')->willReturn($db);
125+
$db->expects($this->exactly(3))->method('select')->willReturn($parentSelect);
126+
$db->expects($this->exactly(3))->method('fetchCol')->willReturn([1]);
127+
$this->resource->expects($this->exactly(3))->method('getConnection')->willReturn($db);
127128

128129
$this->conditionsHelper->expects($this->exactly(3))->method('decode')->willReturn([]);
129130
$this->rule->expects($this->exactly(3))->method('loadPost');

0 commit comments

Comments
 (0)