14
14
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
15
15
use Magento \CatalogInventory \Helper \Stock ;
16
16
use Magento \CatalogWidget \Model \Rule ;
17
- use Magento \Framework \DB \Select ;
18
17
use Magento \Framework \Exception \LocalizedException ;
19
18
use Magento \Rule \Model \Condition \Combine ;
20
19
use Magento \Rule \Model \Condition \Sql \Builder ;
@@ -123,35 +122,28 @@ private function getProductCollection(string $conditions): Collection
123
122
}
124
123
125
124
/**
126
- * Retrieve product totals for collection
125
+ * Retrieve count of all disabled products
127
126
*
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
132
129
*/
133
- public function getProductTotals ( string $ conditions ): array
130
+ private function getDisabledCount ( Collection $ baseCollection ): int
134
131
{
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 ;
151
134
$ disabledCollection ->addAttributeToFilter ('status ' , Status::STATUS_DISABLED );
135
+ return $ disabledCollection ->getSize ();
136
+ }
152
137
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 ;
155
147
$ notVisibleCollection ->addAttributeToFilter ('status ' , Status::STATUS_ENABLED );
156
148
$ notVisibleCollection ->addAttributeToFilter (
157
149
'visibility ' ,
@@ -160,29 +152,70 @@ public function getProductTotals(string $conditions): array
160
152
Visibility::VISIBILITY_IN_SEARCH
161
153
]
162
154
);
155
+ return $ notVisibleCollection ->getSize ();
156
+ }
163
157
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
+ {
164
167
// Retrieve in stock products, then subtract them from the total
165
- $ outOfStockCollection = clone $ collection ;
168
+ $ outOfStockCollection = clone $ baseCollection ;
166
169
$ this ->stockFilter ->addIsInStockFilterToCollection ($ outOfStockCollection );
167
170
// Remove existing stock_status where condition from query
168
- $ outOfStockWhere = $ outOfStockCollection ->getSelect ()->getPart (Select:: WHERE );
171
+ $ outOfStockWhere = $ outOfStockCollection ->getSelect ()->getPart (' where ' );
169
172
$ outOfStockWhere = array_filter (
170
173
$ outOfStockWhere ,
171
174
function ($ whereCondition ) {
172
175
return !stristr ($ whereCondition , 'stock_status ' );
173
176
}
174
177
);
175
- $ outOfStockCollection ->getSelect ()->setPart (Select:: WHERE , $ outOfStockWhere );
178
+ $ outOfStockCollection ->getSelect ()->setPart (' where ' , $ outOfStockWhere );
176
179
$ outOfStockCollection ->getSelect ()->where (
177
180
'stock_status_index.stock_status = ? ' ,
178
181
\Magento \CatalogInventory \Model \Stock \Status::STATUS_OUT_OF_STOCK
179
182
);
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 );
180
213
181
214
return [
182
215
'total ' => $ collection ->getSize (),
183
- 'disabled ' => $ disabledCollection -> getSize () ,
184
- 'notVisible ' => $ notVisibleCollection -> getSize () ,
185
- 'outOfStock ' => $ outOfStockCollection -> getSize () ,
216
+ 'disabled ' => $ disabledCount ,
217
+ 'notVisible ' => $ notVisibleCount ,
218
+ 'outOfStock ' => $ outOfStockCount ,
186
219
];
187
220
}
188
221
}
0 commit comments