Skip to content

Commit be2356c

Browse files
committed
PB-107: Display total number of products matched into ProductsList
- Don’t make request if condition hasn’t changed - Exclude disabled products from not visible - Handle exceptions within controller
1 parent 344bccf commit be2356c

File tree

3 files changed

+87
-56
lines changed

3 files changed

+87
-56
lines changed

app/code/Magento/PageBuilder/Controller/Adminhtml/Form/Element/ProductTotals.php

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -133,59 +133,72 @@ private function createCollection()
133133
*/
134134
public function execute()
135135
{
136-
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
137-
$collection = $this->createCollection();
138-
139-
// Exclude any linked products, e.g. simple products assigned to a configurable, bundle or group
140-
$collection->getSelect()->joinLeft(
141-
['super_link_table' => $collection->getTable('catalog_product_super_link')],
142-
'super_link_table.product_id = e.entity_id',
143-
['product_id']
144-
)->joinLeft(
145-
['link_table' => $collection->getTable('catalog_product_link')],
146-
'link_table.product_id = e.entity_id',
147-
['product_id']
148-
)->where('link_table.product_id IS NULL OR super_link_table.product_id IS NULL');
149-
150-
// Retrieve all disabled products
151-
$disabledCollection = clone $collection;
152-
$disabledCollection->addAttributeToFilter('status', Status::STATUS_DISABLED);
153-
154-
// Retrieve all not visible individually products
155-
$notVisibleCollection = clone $collection;
156-
$notVisibleCollection->addAttributeToFilter(
157-
'visibility',
158-
[
159-
Visibility::VISIBILITY_NOT_VISIBLE,
160-
Visibility::VISIBILITY_IN_SEARCH
161-
]
162-
);
163-
164-
// Retrieve in stock products, then subtract them from the total
165-
$outOfStockCollection = clone $collection;
166-
$this->stockFilter->addIsInStockFilterToCollection($outOfStockCollection);
167-
// Remove existing stock_status where condition from query
168-
$outOfStockWhere = $outOfStockCollection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);
169-
$outOfStockWhere = array_filter(
170-
$outOfStockWhere,
171-
function ($whereCondition) {
172-
return !stristr($whereCondition, 'stock_status');
173-
}
174-
);
175-
$outOfStockCollection->getSelect()->setPart(\Magento\Framework\DB\Select::WHERE, $outOfStockWhere);
176-
$outOfStockCollection->getSelect()->where(
177-
'stock_status_index.stock_status = ?',
178-
\Magento\CatalogInventory\Model\Stock\Status::STATUS_OUT_OF_STOCK
179-
);
180-
181-
return $this->jsonFactory->create()
182-
->setData(
136+
try {
137+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
138+
$collection = $this->createCollection();
139+
140+
// Exclude any linked products, e.g. simple products assigned to a configurable, bundle or group
141+
$collection->getSelect()->joinLeft(
142+
['super_link_table' => $collection->getTable('catalog_product_super_link')],
143+
'super_link_table.product_id = e.entity_id',
144+
['product_id']
145+
)->joinLeft(
146+
['link_table' => $collection->getTable('catalog_product_link')],
147+
'link_table.product_id = e.entity_id',
148+
['product_id']
149+
)->where('link_table.product_id IS NULL OR super_link_table.product_id IS NULL');
150+
151+
// Retrieve all disabled products
152+
$disabledCollection = clone $collection;
153+
$disabledCollection->addAttributeToFilter('status', Status::STATUS_DISABLED);
154+
155+
// Retrieve all not visible individually products
156+
$notVisibleCollection = clone $collection;
157+
$notVisibleCollection->addAttributeToFilter('status', Status::STATUS_ENABLED);
158+
$notVisibleCollection->addAttributeToFilter(
159+
'visibility',
183160
[
184-
'total' => $collection->getSize(),
185-
'disabled' => $disabledCollection->getSize(),
186-
'notVisible' => $notVisibleCollection->getSize(),
187-
'outOfStock' => $outOfStockCollection->getSize(),
161+
Visibility::VISIBILITY_NOT_VISIBLE,
162+
Visibility::VISIBILITY_IN_SEARCH
188163
]
189164
);
165+
166+
// Retrieve in stock products, then subtract them from the total
167+
$outOfStockCollection = clone $collection;
168+
$this->stockFilter->addIsInStockFilterToCollection($outOfStockCollection);
169+
// Remove existing stock_status where condition from query
170+
$outOfStockWhere = $outOfStockCollection->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);
171+
$outOfStockWhere = array_filter(
172+
$outOfStockWhere,
173+
function ($whereCondition) {
174+
return !stristr($whereCondition, 'stock_status');
175+
}
176+
);
177+
$outOfStockCollection->getSelect()->setPart(\Magento\Framework\DB\Select::WHERE, $outOfStockWhere);
178+
$outOfStockCollection->getSelect()->where(
179+
'stock_status_index.stock_status = ?',
180+
\Magento\CatalogInventory\Model\Stock\Status::STATUS_OUT_OF_STOCK
181+
);
182+
183+
return $this->jsonFactory->create()
184+
->setData(
185+
[
186+
'total' => $collection->getSize(),
187+
'disabled' => $disabledCollection->getSize(),
188+
'notVisible' => $notVisibleCollection->getSize(),
189+
'outOfStock' => $outOfStockCollection->getSize(),
190+
]
191+
);
192+
} catch (\Exception $e) {
193+
return $this->jsonFactory->create()
194+
->setData(
195+
[
196+
'total' => 0,
197+
'disabled' => 0,
198+
'notVisible' => 0,
199+
'outOfStock' => 0,
200+
]
201+
);
202+
}
190203
}
191204
}

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/product-totals.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ define([
2121
totalDisabledProducts: 0,
2222
totalNotVisibleProducts: 0,
2323
totalOutOfStockProducts: 0,
24+
previousConditions: false,
2425
listens: {
2526
conditionOption: 'updateProductTotals',
2627
conditionValue: 'updateProductTotals',
@@ -41,14 +42,19 @@ define([
4142
jqXHR: null
4243
},
4344

44-
initialize: function() {
45+
/** @inheritdoc */
46+
initialize: function () {
4547
$('.cms-page-edit .modals-wrapper').on('modalclosed', function () {
4648
this.abortRunningRequest();
4749
}.bind(this));
50+
4851
return this._super();
4952
},
5053

51-
abortRunningRequest: function() {
54+
/**
55+
* Abort running Ajax request
56+
*/
57+
abortRunningRequest: function () {
5258
if (this.jqXHR && this.jqXHR.readyState !== 4) {
5359
this.jqXHR.abort();
5460
}
@@ -61,8 +67,14 @@ define([
6167
'totalOutOfStockProducts loading');
6268
},
6369

70+
/**
71+
* If we haven't aborted the request, continue and display the error
72+
*
73+
* @param {Object} jqXHR
74+
*/
6475
callSuperError: function (jqXHR) {
6576
var superError = $.ajaxSettings.error.bind(window, jqXHR);
77+
6678
superError();
6779
},
6880

@@ -84,6 +96,12 @@ define([
8496
_.extend(this.formData, this.conditionValue);
8597
conditionsDataProcessor(this.formData, this.conditionOption + '_source');
8698

99+
// Store the previous conditions so we don't update the totals when nothing has changed
100+
if (this.previousConditions === this.formData['conditions_encoded']) {
101+
return;
102+
}
103+
this.previousConditions = this.formData['conditions_encoded'];
104+
87105
this.loading(true);
88106
this.abortRunningRequest();
89107
this.jqXHR = $.ajax({
@@ -94,7 +112,7 @@ define([
94112
},
95113
error: function (jqXHR) {
96114
if (jqXHR.statusText !== 'abort') {
97-
this.callSuperError(jqXHR)
115+
this.callSuperError(jqXHR);
98116
}
99117
}.bind(this)
100118
}).done(function (response) {

dev/tests/integration/testsuite/Magento/PageBuilder/Controller/Adminhtml/Form/Element/ProductTotalsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function productDataProvider()
7171
],
7272
[0, 0, 0, 0]
7373
],
74-
[ // #1 category with 4 products, 3 disabled, 3 not visible
74+
[ // #1 category with 4 products, 3 disabled, 3 not visible (but 2 not visible disabled)
7575
['1' => [
7676
'aggregator' => 'all',
7777
'new_child' => '',
@@ -85,7 +85,7 @@ public function productDataProvider()
8585
'value' => '3'
8686
]
8787
],
88-
[8, 3, 3, 1]
88+
[8, 3, 1, 1]
8989
],
9090
[ // #2 sku with no matches
9191
['1' => [

0 commit comments

Comments
 (0)