Skip to content

Commit 52225ca

Browse files
bubasumaeug123
authored andcommitted
PB-525: [2.3.6][Integration] Test Magento.CatalogPageBuilder.Model.Catalog.SortingTest.testSortOptions with data set #0 fails on Jenkins
- cherry-picked from MC-31247: [Page Builder] Conditions "Price" does not work for Configurable products - Fix catalog products list widget is not showing configurable products if condition is using price
1 parent ee7bba0 commit 52225ca

File tree

4 files changed

+161
-47
lines changed

4 files changed

+161
-47
lines changed

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

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,21 @@ private function updateAnchorCategoryConditions(array $condition): array
134134
* Retrieve product collection based on provided conditions
135135
*
136136
* @param string $conditions
137+
* @param bool $usePriceIndex use minimal price from price index to filter by price.
138+
* If TRUE only enabled products will be returned, as price indexer does not include disabled products
137139
* @return Collection
138140
* @throws LocalizedException
139141
*/
140-
private function getProductCollection(string $conditions): Collection
142+
private function getProductCollection(string $conditions, bool $usePriceIndex = true): Collection
141143
{
142144
/** @var $collection Collection */
143145
$collection = $this->productCollectionFactory->create();
146+
if ($usePriceIndex && strpos($conditions, '"attribute":"price"') !== false) {
147+
$collection->addMinimalPrice();
148+
}
144149

145-
/** @var Combine $collectionConditions */
146-
$collectionConditions = $this->decodeConditions($conditions);
147-
$collectionConditions->collectValidatedAttributes($collection);
148-
$this->sqlBuilder->attachConditionToCollection($collection, $collectionConditions);
150+
$collection = $this->applyConditionsToCollection($conditions, $collection);
151+
$collection = $this->excludeLinkedProducts($collection);
149152

150153
/**
151154
* Prevent retrieval of duplicate records. This may occur when multiselect product attribute matches
@@ -156,38 +159,90 @@ private function getProductCollection(string $conditions): Collection
156159
return $collection;
157160
}
158161

162+
/**
163+
* Retrieve count of all enabled products
164+
*
165+
* @param string $conditions
166+
* @return int number of enabled products
167+
*/
168+
private function getEnabledCount(string $conditions): int
169+
{
170+
$collection = $this->getProductCollection($conditions, true);
171+
$collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
172+
return $collection->getSize();
173+
}
174+
159175
/**
160176
* Retrieve count of all disabled products
161177
*
162-
* @param Collection $baseCollection
178+
* @param string $conditions
163179
* @return int number of disabled products
164180
*/
165-
private function getDisabledCount(Collection $baseCollection): int
181+
private function getDisabledCount(string $conditions): int
166182
{
167-
/** @var Collection $disabledCollection */
168-
$disabledCollection = clone $baseCollection;
169-
$disabledCollection->addAttributeToFilter('status', Status::STATUS_DISABLED);
170-
return $disabledCollection->getSize();
183+
$collection = $this->getProductCollection($conditions, false);
184+
$collection->addAttributeToFilter('status', Status::STATUS_DISABLED);
185+
return $collection->getSize();
171186
}
172187

173188
/**
174189
* Retrieve count of all not visible individually products
175190
*
176-
* @param Collection $baseCollection
191+
* @param string $conditions
177192
* @return int number of products not visible individually
178193
*/
179-
private function getNotVisibleCount(Collection $baseCollection): int
194+
private function getNotVisibleCount(string $conditions): int
180195
{
181-
$notVisibleCollection = clone $baseCollection;
182-
$notVisibleCollection->addAttributeToFilter('status', Status::STATUS_ENABLED);
183-
$notVisibleCollection->addAttributeToFilter(
196+
$collection = $this->getProductCollection($conditions, true);
197+
$collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
198+
$collection->addAttributeToFilter(
184199
'visibility',
185200
[
186201
Visibility::VISIBILITY_NOT_VISIBLE,
187202
Visibility::VISIBILITY_IN_SEARCH
188203
]
189204
);
190-
return $notVisibleCollection->getSize();
205+
return $collection->getSize();
206+
}
207+
208+
/**
209+
* Exclude any linked products, e.g. simple products assigned to a configurable, bundle or group
210+
*
211+
* @param Collection $collection
212+
* @return Collection
213+
*/
214+
private function excludeLinkedProducts(Collection $collection): Collection
215+
{
216+
$collection->getSelect()
217+
->joinLeft(
218+
['super_link_table' => $collection->getTable('catalog_product_super_link')],
219+
'super_link_table.product_id = e.entity_id',
220+
['product_id']
221+
)
222+
->joinLeft(
223+
['link_table' => $collection->getTable('catalog_product_link')],
224+
'link_table.product_id = e.entity_id',
225+
['product_id']
226+
)
227+
->where('link_table.product_id IS NULL OR super_link_table.product_id IS NULL');
228+
return $collection;
229+
}
230+
231+
/**
232+
* Apply conditions to collection
233+
*
234+
* @param string $conditions
235+
* @param Collection $collection
236+
* @return Collection
237+
* @throws LocalizedException
238+
*/
239+
private function applyConditionsToCollection(string $conditions, Collection $collection): Collection
240+
{
241+
/** @var Combine $collectionConditions */
242+
$collectionConditions = $this->decodeConditions($conditions);
243+
$collectionConditions->collectValidatedAttributes($collection);
244+
$this->sqlBuilder->attachConditionToCollection($collection, $collectionConditions);
245+
return $collection;
191246
}
192247

193248
/**
@@ -200,25 +255,12 @@ private function getNotVisibleCount(Collection $baseCollection): int
200255
*/
201256
public function getProductTotals(string $conditions): array
202257
{
203-
/** @var Collection $collection */
204-
$collection = $this->getProductCollection($conditions);
205-
206-
// Exclude any linked products, e.g. simple products assigned to a configurable, bundle or group
207-
$collection->getSelect()->joinLeft(
208-
['super_link_table' => $collection->getTable('catalog_product_super_link')],
209-
'super_link_table.product_id = e.entity_id',
210-
['product_id']
211-
)->joinLeft(
212-
['link_table' => $collection->getTable('catalog_product_link')],
213-
'link_table.product_id = e.entity_id',
214-
['product_id']
215-
)->where('link_table.product_id IS NULL OR super_link_table.product_id IS NULL');
216-
217-
$disabledCount = $this->getDisabledCount($collection);
218-
$notVisibleCount = $this->getNotVisibleCount($collection);
258+
$enabledCount = $this->getEnabledCount($conditions);
259+
$disabledCount = $this->getDisabledCount($conditions);
260+
$notVisibleCount = $this->getNotVisibleCount($conditions);
219261

220262
return [
221-
'total' => $collection->getSize(),
263+
'total' => $enabledCount + $disabledCount,
222264
'disabled' => $disabledCount,
223265
'notVisible' => $notVisibleCount,
224266
];
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Model\Catalog\Sorting;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\Framework\DB\Select;
12+
use Magento\Framework\Phrase;
13+
14+
/**
15+
* Sort catalog products by price
16+
*/
17+
class Price implements OptionInterface
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $label;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $sortDirection;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $secondarySortDirection;
33+
34+
/**
35+
* @param string $label
36+
* @param string $sortDirection
37+
* @param string $secondarySortDirection
38+
*/
39+
public function __construct(
40+
string $label,
41+
string $sortDirection = Select::SQL_ASC,
42+
?string $secondarySortDirection = null
43+
) {
44+
$this->label = $label;
45+
$this->sortDirection = $sortDirection;
46+
$this->secondarySortDirection = $secondarySortDirection ?? $sortDirection;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public function sort(Collection $collection): Collection
53+
{
54+
$collection->getSelect()->reset(Select::ORDER);
55+
if ($collection->getLimitationFilters()->isUsingPriceIndex()) {
56+
$collection->getSelect()->order("price_index.min_price $this->sortDirection");
57+
} else {
58+
$collection->addAttributeToSort('price', $this->sortDirection);
59+
}
60+
$collection->addAttributeToSort('entity_id', $this->secondarySortDirection);
61+
return $collection;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function getLabel(): Phrase
68+
{
69+
return __($this->label);
70+
}
71+
}

app/code/Magento/PageBuilder/Ui/DataProvider/Product/ProductCollection.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,4 @@ public function setVisibility($visibility)
2929

3030
return $this;
3131
}
32-
33-
/**
34-
* @inheritdoc
35-
*/
36-
protected function _productLimitationJoinPrice()
37-
{
38-
$this->_productLimitationFilters->setUsePriceIndex($this->getStoreId() !== Store::DEFAULT_STORE_ID);
39-
return $this->_productLimitationPrice(false);
40-
}
4132
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,12 @@
236236
<arguments>
237237
<argument name="label" xsi:type="string">Price: high to low</argument>
238238
<argument name="sortDirection" xsi:type="const">\Magento\Framework\DB\Select::SQL_DESC</argument>
239-
<argument name="attributeField" xsi:type="string">price</argument>
240239
</arguments>
241240
</virtualType>
242241
<virtualType name="Magento\PageBuilder\Model\Catalog\Sorting\Price\LowToHigh" type="Magento\PageBuilder\Model\Catalog\Sorting\SimpleOption">
243242
<arguments>
244243
<argument name="label" xsi:type="string">Price: low to high</argument>
245244
<argument name="sortDirection" xsi:type="const">\Magento\Framework\DB\Select::SQL_ASC</argument>
246-
<argument name="attributeField" xsi:type="string">price</argument>
247245
</arguments>
248246
</virtualType>
249247
<virtualType name="Magento\PageBuilder\Model\Catalog\Sorting\Position" type="Magento\PageBuilder\Model\Catalog\Sorting\SimpleOption">
@@ -259,6 +257,18 @@
259257
<argument name="label" xsi:type="string">Position</argument>
260258
</arguments>
261259
</virtualType>
260+
<virtualType name="Magento\PageBuilder\Model\Catalog\Sorting\Price\Descending" type="Magento\PageBuilder\Model\Catalog\Sorting\Price">
261+
<arguments>
262+
<argument name="label" xsi:type="string">Price: high to low</argument>
263+
<argument name="sortDirection" xsi:type="const">\Magento\Framework\DB\Select::SQL_DESC</argument>
264+
</arguments>
265+
</virtualType>
266+
<virtualType name="Magento\PageBuilder\Model\Catalog\Sorting\Price\Ascending" type="Magento\PageBuilder\Model\Catalog\Sorting\Price">
267+
<arguments>
268+
<argument name="label" xsi:type="string">Price: low to high</argument>
269+
<argument name="sortDirection" xsi:type="const">\Magento\Framework\DB\Select::SQL_ASC</argument>
270+
</arguments>
271+
</virtualType>
262272
<type name="Magento\PageBuilder\Model\Catalog\Sorting">
263273
<arguments>
264274
<argument name="sortClasses" xsi:type="array">
@@ -272,8 +282,8 @@
272282
<item name="sku_descending" xsi:type="string">Magento\PageBuilder\Model\Catalog\Sorting\Sku\Descending</item>
273283
<item name="low_stock_first" xsi:type="string">Magento\PageBuilder\Model\Catalog\Sorting\Stock\Ascending</item>
274284
<item name="high_stock_first" xsi:type="string">Magento\PageBuilder\Model\Catalog\Sorting\Stock\Descending</item>
275-
<item name="price_high_to_low" xsi:type="string">Magento\PageBuilder\Model\Catalog\Sorting\Price\HighToLow</item>
276-
<item name="price_low_to_high" xsi:type="string">Magento\PageBuilder\Model\Catalog\Sorting\Price\LowToHigh</item>
285+
<item name="price_high_to_low" xsi:type="string">Magento\PageBuilder\Model\Catalog\Sorting\Price\Descending</item>
286+
<item name="price_low_to_high" xsi:type="string">Magento\PageBuilder\Model\Catalog\Sorting\Price\Ascending</item>
277287
</argument>
278288
</arguments>
279289
</type>

0 commit comments

Comments
 (0)