@@ -57,15 +57,14 @@ class ProductTotals extends \Magento\Backend\App\Action implements HttpPostActio
57
57
private $ stockFilter ;
58
58
59
59
/**
60
- * Constructor.
61
- *
62
60
* @param \Magento\Backend\App\Action\Context $context
63
61
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
64
62
* @param \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder
65
63
* @param \Magento\CatalogWidget\Model\Rule $rule
66
64
* @param \Magento\Widget\Helper\Conditions $conditionsHelper
67
65
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
68
66
* @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
67
+ * @param \Magento\CatalogInventory\Helper\Stock $stockFilter
69
68
*/
70
69
public function __construct (
71
70
\Magento \Backend \App \Action \Context $ context ,
@@ -115,11 +114,12 @@ private function getConditions()
115
114
/**
116
115
* Prepare and return product collection
117
116
*
118
- * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
117
+ * @return Collection
118
+ * @throws \Magento\Framework\Exception\LocalizedException
119
119
*/
120
120
private function createCollection ()
121
121
{
122
- /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\ Collection */
122
+ /** @var $collection Collection */
123
123
$ collection = $ this ->productCollectionFactory ->create ();
124
124
125
125
/** @var \Magento\Rule\Model\Condition\Combine $conditions */
@@ -143,6 +143,8 @@ public function execute()
143
143
{
144
144
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
145
145
$ collection = $ this ->createCollection ();
146
+
147
+ // Exclude any linked products, e.g. simple products assigned to a configurable, bundle or group
146
148
$ collection ->getSelect ()->joinLeft (
147
149
['super_link_table ' => $ collection ->getTable ('catalog_product_super_link ' )],
148
150
'super_link_table.product_id = e.entity_id ' ,
@@ -153,20 +155,44 @@ public function execute()
153
155
['product_id ' ]
154
156
)->where ('link_table.product_id IS NULL OR super_link_table.product_id IS NULL ' );
155
157
156
- // Clone the collection before we add enabled status filter
157
- $ disabledProductsCollection = clone $ collection ;
158
- $ disabledProductsCollection ->addAttributeToFilter ('status ' , Status::STATUS_DISABLED );
159
-
160
- // Only display enabled products in totals count
161
- $ collection ->addAttributeToFilter ('status ' , Status::STATUS_ENABLED );
162
- $ collection ->setVisibility (Visibility::VISIBILITY_BOTH );
163
- $ this ->stockFilter ->addIsInStockFilterToCollection ($ collection );
158
+ // Retrieve all disabled products
159
+ $ disabledCollection = clone $ collection ;
160
+ $ disabledCollection ->addAttributeToFilter ('status ' , Status::STATUS_DISABLED );
161
+
162
+ // Retrieve all not visible individually products
163
+ $ notVisibleCollection = clone $ collection ;
164
+ $ notVisibleCollection ->addAttributeToFilter (
165
+ 'visibility ' ,
166
+ [
167
+ Visibility::VISIBILITY_NOT_VISIBLE ,
168
+ Visibility::VISIBILITY_IN_SEARCH
169
+ ]
170
+ );
171
+
172
+ // Retrieve in stock products, then subtract them from the total
173
+ $ outOfStockCollection = clone $ collection ;
174
+ $ this ->stockFilter ->addIsInStockFilterToCollection ($ outOfStockCollection );
175
+ // Remove existing stock_status where condition from query
176
+ $ outOfStockWhere = $ outOfStockCollection ->getSelect ()->getPart (\Magento \Framework \DB \Select::WHERE );
177
+ $ outOfStockWhere = array_filter (
178
+ $ outOfStockWhere ,
179
+ function ($ whereCondition ) {
180
+ return !stristr ($ whereCondition , 'stock_status ' );
181
+ }
182
+ );
183
+ $ outOfStockCollection ->getSelect ()->setPart (\Magento \Framework \DB \Select::WHERE , $ outOfStockWhere );
184
+ $ outOfStockCollection ->getSelect ()->where (
185
+ 'stock_status_index.stock_status = ? ' ,
186
+ \Magento \CatalogInventory \Model \Stock \Status::STATUS_OUT_OF_STOCK
187
+ );
164
188
165
189
return $ this ->jsonFactory ->create ()
166
190
->setData (
167
191
[
168
192
'total ' => $ collection ->getSize (),
169
- 'disabled ' => $ disabledProductsCollection ->getSize ()
193
+ 'disabled ' => $ disabledCollection ->getSize (),
194
+ 'notVisible ' => $ notVisibleCollection ->getSize (),
195
+ 'outOfStock ' => $ outOfStockCollection ->getSize (),
170
196
]
171
197
);
172
198
}
0 commit comments