Skip to content

Commit 4f319c2

Browse files
committed
PB-235: Story Bug for PB-107: Adding Condition Filter will Not Update Total until After Saving Edit Form & Reopening
- fix static & integration failures
1 parent 7a98308 commit 4f319c2

File tree

2 files changed

+82
-46
lines changed

2 files changed

+82
-46
lines changed

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

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1515
use Magento\CatalogInventory\Helper\Stock;
1616
use Magento\CatalogWidget\Model\Rule;
17-
use Magento\Framework\DB\Select;
1817
use Magento\Framework\Exception\LocalizedException;
1918
use Magento\Rule\Model\Condition\Combine;
2019
use Magento\Rule\Model\Condition\Sql\Builder;
@@ -123,35 +122,28 @@ private function getProductCollection(string $conditions): Collection
123122
}
124123

125124
/**
126-
* Retrieve product totals for collection
125+
* Retrieve count of all disabled products
127126
*
128-
* @param string $conditions
129-
* @return array
130-
* @throws LocalizedException
131-
* @throws Zend_Db_Select_Exception
127+
* @param Collection $baseCollection
128+
* @return int number of disabled products
132129
*/
133-
public function getProductTotals(string $conditions): array
130+
private function getDisabledCount(Collection $baseCollection): int
134131
{
135-
/** @var Collection $collection */
136-
$collection = $this->getProductCollection($conditions);
137-
138-
// Exclude any linked products, e.g. simple products assigned to a configurable, bundle or group
139-
$collection->getSelect()->joinLeft(
140-
['super_link_table' => $collection->getTable('catalog_product_super_link')],
141-
'super_link_table.product_id = e.entity_id',
142-
['product_id']
143-
)->joinLeft(
144-
['link_table' => $collection->getTable('catalog_product_link')],
145-
'link_table.product_id = e.entity_id',
146-
['product_id']
147-
)->where('link_table.product_id IS NULL OR super_link_table.product_id IS NULL');
148-
149-
// Retrieve all disabled products
150-
$disabledCollection = clone $collection;
132+
/** @var Collection $disabledCollection */
133+
$disabledCollection = clone $baseCollection;
151134
$disabledCollection->addAttributeToFilter('status', Status::STATUS_DISABLED);
135+
return $disabledCollection->getSize();
136+
}
152137

153-
// Retrieve all not visible individually products
154-
$notVisibleCollection = clone $collection;
138+
/**
139+
* Retrieve count of all not visible individually products
140+
*
141+
* @param Collection $baseCollection
142+
* @return int number of products not visible individually
143+
*/
144+
private function getNotVisibleCount(Collection $baseCollection): int
145+
{
146+
$notVisibleCollection = clone $baseCollection;
155147
$notVisibleCollection->addAttributeToFilter('status', Status::STATUS_ENABLED);
156148
$notVisibleCollection->addAttributeToFilter(
157149
'visibility',
@@ -160,29 +152,70 @@ public function getProductTotals(string $conditions): array
160152
Visibility::VISIBILITY_IN_SEARCH
161153
]
162154
);
155+
return $notVisibleCollection->getSize();
156+
}
163157

158+
/**
159+
* Retrieve count of all out of stock products
160+
*
161+
* @param Collection $baseCollection
162+
* @return int number of out of stock products
163+
* @throws Zend_Db_Select_Exception
164+
*/
165+
private function getOutOfStockCount(Collection $baseCollection): int
166+
{
164167
// Retrieve in stock products, then subtract them from the total
165-
$outOfStockCollection = clone $collection;
168+
$outOfStockCollection = clone $baseCollection;
166169
$this->stockFilter->addIsInStockFilterToCollection($outOfStockCollection);
167170
// Remove existing stock_status where condition from query
168-
$outOfStockWhere = $outOfStockCollection->getSelect()->getPart(Select::WHERE);
171+
$outOfStockWhere = $outOfStockCollection->getSelect()->getPart('where');
169172
$outOfStockWhere = array_filter(
170173
$outOfStockWhere,
171174
function ($whereCondition) {
172175
return !stristr($whereCondition, 'stock_status');
173176
}
174177
);
175-
$outOfStockCollection->getSelect()->setPart(Select::WHERE, $outOfStockWhere);
178+
$outOfStockCollection->getSelect()->setPart('where', $outOfStockWhere);
176179
$outOfStockCollection->getSelect()->where(
177180
'stock_status_index.stock_status = ?',
178181
\Magento\CatalogInventory\Model\Stock\Status::STATUS_OUT_OF_STOCK
179182
);
183+
return $outOfStockCollection->getSize();
184+
}
185+
186+
/**
187+
* Retrieve product totals for collection
188+
*
189+
* @param string $conditions
190+
* @return array
191+
* @throws LocalizedException
192+
* @throws Zend_Db_Select_Exception
193+
*/
194+
public function getProductTotals(string $conditions): array
195+
{
196+
/** @var Collection $collection */
197+
$collection = $this->getProductCollection($conditions);
198+
199+
// Exclude any linked products, e.g. simple products assigned to a configurable, bundle or group
200+
$collection->getSelect()->joinLeft(
201+
['super_link_table' => $collection->getTable('catalog_product_super_link')],
202+
'super_link_table.product_id = e.entity_id',
203+
['product_id']
204+
)->joinLeft(
205+
['link_table' => $collection->getTable('catalog_product_link')],
206+
'link_table.product_id = e.entity_id',
207+
['product_id']
208+
)->where('link_table.product_id IS NULL OR super_link_table.product_id IS NULL');
209+
210+
$disabledCount = $this->getDisabledCount($collection);
211+
$notVisibleCount = $this->getNotVisibleCount($collection);
212+
$outOfStockCount = $this->getOutOfStockCount($collection);
180213

181214
return [
182215
'total' => $collection->getSize(),
183-
'disabled' => $disabledCollection->getSize(),
184-
'notVisible' => $notVisibleCollection->getSize(),
185-
'outOfStock' => $outOfStockCollection->getSize(),
216+
'disabled' => $disabledCount,
217+
'notVisible' => $notVisibleCount,
218+
'outOfStock' => $outOfStockCount,
186219
];
187220
}
188221
}

dev/tests/integration/testsuite/Magento/PageBuilder/Model/Catalog/ProductTotalsTest.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\PageBuilder\Model\Catalog;
810

11+
use Magento\Framework\Exception\LocalizedException;
912
use Magento\TestFramework\Helper\Bootstrap;
13+
use Zend_Db_Select_Exception;
1014

1115
/**
1216
* Class ProductTotalsTest
17+
*
18+
* @magentoAppIsolation enabled
19+
* @magentoDataFixture Magento/PageBuilder/_files/product_totals/products.php
1320
*/
1421
class ProductTotalsTest extends \PHPUnit\Framework\TestCase
1522
{
@@ -28,17 +35,19 @@ protected function setUp()
2835
}
2936

3037
/**
31-
* @param string condition
38+
* @param array condition
3239
* @param int $expectedTotals
3340
* @dataProvider productDataProvider
41+
* @throws LocalizedException
42+
* @throws Zend_Db_Select_Exception
3443
*/
3544
public function testProductTotals(
3645
$condition,
3746
$expectedTotals
3847
) {
3948
$this->assertEquals(
40-
array_values($this->productTotals->getProductTotals($condition)),
41-
$expectedTotals
49+
$expectedTotals,
50+
array_values($this->productTotals->getProductTotals(json_encode($condition)))
4251
);
4352
}
4453

@@ -61,8 +70,7 @@ public function productDataProvider()
6170
'attribute' => 'category_ids',
6271
'value' => '4'
6372
]
64-
],
65-
[0, 0, 0, 0]
73+
], [0, 0, 0, 0]
6674
],
6775
[ // #1 category with 4 products, 3 disabled, 3 not visible (but 2 not visible disabled)
6876
['1' => [
@@ -77,8 +85,7 @@ public function productDataProvider()
7785
'attribute' => 'category_ids',
7886
'value' => '3'
7987
]
80-
],
81-
[8, 3, 1, 1]
88+
], [8, 3, 1, 1]
8289
],
8390
[ // #2 sku with no matches
8491
['1' => [
@@ -93,8 +100,7 @@ public function productDataProvider()
93100
'attribute' => 'sku',
94101
'value' => 'shoes'
95102
]
96-
],
97-
[0, 0, 0, 0]
103+
], [0, 0, 0, 0]
98104
],
99105
[ // #3 sku with 2 matches, 1 disabled, 1 not visible, 1 out of stock
100106
['1' => [
@@ -109,8 +115,7 @@ public function productDataProvider()
109115
'attribute' => 'sku',
110116
'value' => 'not-visible-on-storefront, disabled-product, out-of-stock'
111117
]
112-
],
113-
[3, 1, 1, 1]
118+
], [3, 1, 1, 1]
114119
],
115120
[ // #4 condition with no matches
116121
['1' => [
@@ -125,8 +130,7 @@ public function productDataProvider()
125130
'attribute' => 'price',
126131
'value' => '10000'
127132
]
128-
],
129-
[0, 0, 0, 0]
133+
], [0, 0, 0, 0]
130134
],
131135
[ // #5 condition with 3 matches, 1 disabled, 1 not visible
132136
['1' => [
@@ -141,8 +145,7 @@ public function productDataProvider()
141145
'attribute' => 'price',
142146
'value' => '20'
143147
]
144-
],
145-
[3, 1, 1, 0]
148+
], [3, 1, 1, 0]
146149
],
147150
];
148151
}

0 commit comments

Comments
 (0)