@@ -133,59 +133,72 @@ private function createCollection()
133
133
*/
134
134
public function execute ()
135
135
{
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 ' ,
183
160
[
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
188
163
]
189
164
);
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
+ }
190
203
}
191
204
}
0 commit comments