@@ -50,6 +50,11 @@ class ProductTotals extends \Magento\Backend\App\Action implements HttpPostActio
50
50
*/
51
51
private $ jsonFactory ;
52
52
53
+ /**
54
+ * @var \Magento\CatalogInventory\Helper\Stock
55
+ */
56
+ private $ stockFilter ;
57
+
53
58
/**
54
59
* Constructor.
55
60
*
@@ -68,14 +73,16 @@ public function __construct(
68
73
\Magento \CatalogWidget \Model \Rule $ rule ,
69
74
\Magento \Widget \Helper \Conditions $ conditionsHelper ,
70
75
\Magento \Catalog \Api \CategoryRepositoryInterface $ categoryRepository ,
71
- \Magento \Framework \Controller \Result \JsonFactory $ jsonFactory
76
+ \Magento \Framework \Controller \Result \JsonFactory $ jsonFactory ,
77
+ \Magento \CatalogInventory \Helper \Stock $ stockFilter
72
78
) {
73
79
$ this ->productCollectionFactory = $ productCollectionFactory ;
74
80
$ this ->sqlBuilder = $ sqlBuilder ;
75
81
$ this ->rule = $ rule ;
76
82
$ this ->conditionsHelper = $ conditionsHelper ;
77
83
$ this ->categoryRepository = $ categoryRepository ;
78
84
$ this ->jsonFactory = $ jsonFactory ;
85
+ $ this ->stockFilter = $ stockFilter ;
79
86
parent ::__construct ($ context );
80
87
}
81
88
@@ -135,26 +142,30 @@ public function execute()
135
142
{
136
143
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
137
144
$ collection = $ this ->createCollection ();
145
+ $ this ->stockFilter ->addInStockFilterToCollection ($ collection );
138
146
$ collection ->getSelect ()->joinLeft (
139
147
['super_link_table ' => $ collection ->getTable ('catalog_product_super_link ' )],
140
148
'super_link_table.product_id = e.entity_id ' ,
141
149
['product_id ' ]
142
150
)->joinLeft (
143
151
['link_table ' => $ collection ->getTable ('catalog_product_link ' )],
144
- 'link_table.linked_product_id = e.entity_id ' ,
152
+ 'link_table.product_id = e.entity_id ' ,
145
153
['product_id ' ]
146
- )->where ('link_table.linked_product_id IS NULL AND super_link_table.product_id IS NULL ' );
154
+ )->where ('link_table.product_id IS NULL OR super_link_table.product_id IS NULL ' );
155
+
156
+ // Clone the collection before we add enabled status filter
157
+ $ disabledProductsCollection = clone $ collection ;
158
+ $ disabledProductsCollection ->addAttributeToFilter ('status ' , Status::STATUS_DISABLED );
147
159
148
- $ totalProducts = $ collection ->getSize ();
149
- $ disabledProducts = $ collection
150
- ->addAttributeToFilter ('status ' , Status::STATUS_DISABLED )
151
- ->getSize ();
160
+ // Only display enabled products in totals count
161
+ $ collection ->addAttributeToFilter ('status ' , Status::STATUS_ENABLED );
162
+ $ collection ->setVisibility (\Magento \Catalog \Model \Product \Visibility::VISIBILITY_BOTH );
152
163
153
164
return $ this ->jsonFactory ->create ()
154
165
->setData (
155
166
[
156
- 'total ' => $ totalProducts ,
157
- 'disabled ' => $ disabledProducts
167
+ 'total ' => $ collection -> getSize () ,
168
+ 'disabled ' => $ disabledProductsCollection -> getSize ()
158
169
]
159
170
);
160
171
}
0 commit comments