8
8
9
9
namespace Magento \PageBuilder \Controller \Adminhtml \Form \Element ;
10
10
11
- use Magento \Catalog \Model \Product \Attribute \Source \Status ;
12
- use Magento \Catalog \Model \Product \Visibility ;
13
11
use Magento \Framework \App \Action \HttpPostActionInterface ;
14
12
15
13
/**
@@ -20,185 +18,54 @@ class ProductTotals extends \Magento\Backend\App\Action implements HttpPostActio
20
18
const ADMIN_RESOURCE = 'Magento_Catalog::products ' ;
21
19
22
20
/**
23
- * Product collection factory
24
- *
25
- * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
21
+ * @var \Magento\PageBuilder\Model\Catalog\ProductTotals
26
22
*/
27
- private $ productCollectionFactory ;
28
-
29
- /**
30
- * @var \Magento\Rule\Model\Condition\Sql\Builder
31
- */
32
- private $ sqlBuilder ;
33
-
34
- /**
35
- * @var \Magento\CatalogWidget\Model\Rule
36
- */
37
- private $ rule ;
38
-
39
- /**
40
- * @var \Magento\Widget\Helper\Conditions
41
- */
42
- private $ conditionsHelper ;
23
+ private $ productTotals ;
43
24
44
25
/**
45
26
* @var \Magento\Framework\Controller\Result\JsonFactory
46
27
*/
47
28
private $ jsonFactory ;
48
29
49
- /**
50
- * @var \Magento\CatalogInventory\Helper\Stock
51
- */
52
- private $ stockFilter ;
53
-
54
30
/**
55
31
* @param \Magento\Backend\App\Action\Context $context
56
- * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
57
- * @param \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder
58
- * @param \Magento\CatalogWidget\Model\Rule $rule
59
- * @param \Magento\Widget\Helper\Conditions $conditionsHelper
32
+ * @param \Magento\PageBuilder\Model\Catalog\ProductTotals $productTotals
60
33
* @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
61
- * @param \Magento\CatalogInventory\Helper\Stock $stockFilter
62
34
*/
63
35
public function __construct (
64
36
\Magento \Backend \App \Action \Context $ context ,
65
- \Magento \Catalog \Model \ResourceModel \Product \CollectionFactory $ productCollectionFactory ,
66
- \Magento \Rule \Model \Condition \Sql \Builder $ sqlBuilder ,
67
- \Magento \CatalogWidget \Model \Rule $ rule ,
68
- \Magento \Widget \Helper \Conditions $ conditionsHelper ,
69
- \Magento \Framework \Controller \Result \JsonFactory $ jsonFactory ,
70
- \Magento \CatalogInventory \Helper \Stock $ stockFilter
37
+ \Magento \PageBuilder \Model \Catalog \ProductTotals $ productTotals ,
38
+ \Magento \Framework \Controller \Result \JsonFactory $ jsonFactory
71
39
) {
72
- $ this ->productCollectionFactory = $ productCollectionFactory ;
73
- $ this ->sqlBuilder = $ sqlBuilder ;
74
- $ this ->rule = $ rule ;
75
- $ this ->conditionsHelper = $ conditionsHelper ;
76
40
$ this ->jsonFactory = $ jsonFactory ;
77
- $ this ->stockFilter = $ stockFilter ;
41
+ $ this ->productTotals = $ productTotals ;
78
42
parent ::__construct ($ context );
79
43
}
80
44
81
- /**
82
- * Get conditions
83
- *
84
- * @return \Magento\Rule\Model\Condition\Combine
85
- */
86
- private function getConditions ()
87
- {
88
- $ conditions = $ this ->getRequest ()->getParam ('conditionValue ' );
89
-
90
- if ($ conditions ) {
91
- $ conditions = $ this ->conditionsHelper ->decode ($ conditions );
92
- }
93
-
94
- foreach ($ conditions as $ key => $ condition ) {
95
- if (!empty ($ condition ['attribute ' ])
96
- && in_array ($ condition ['attribute ' ], ['special_from_date ' , 'special_to_date ' ])
97
- ) {
98
- $ conditions [$ key ]['value ' ] = date ('Y-m-d H:i:s ' , strtotime ($ condition ['value ' ]));
99
- }
100
- }
101
-
102
- $ this ->rule ->loadPost (['conditions ' => $ conditions ]);
103
- return $ this ->rule ->getConditions ();
104
- }
105
-
106
- /**
107
- * Prepare and return product collection
108
- *
109
- * @return Collection
110
- * @throws \Magento\Framework\Exception\LocalizedException
111
- */
112
- private function createCollection ()
113
- {
114
- /** @var $collection Collection */
115
- $ collection = $ this ->productCollectionFactory ->create ();
116
-
117
- /** @var \Magento\Rule\Model\Condition\Combine $conditions */
118
- $ conditions = $ this ->getConditions ();
119
- $ conditions ->collectValidatedAttributes ($ collection );
120
- $ this ->sqlBuilder ->attachConditionToCollection ($ collection , $ conditions );
121
-
122
- /**
123
- * Prevent retrieval of duplicate records. This may occur when multiselect product attribute matches
124
- * several allowed values from condition simultaneously
125
- */
126
- $ collection ->distinct (true );
127
-
128
- return $ collection ;
129
- }
130
-
131
45
/**
132
46
* @inheritdoc
133
47
*/
134
48
public function execute ()
135
49
{
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 ' ,
160
- [
161
- Visibility::VISIBILITY_NOT_VISIBLE ,
162
- Visibility::VISIBILITY_IN_SEARCH
163
- ]
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
- );
50
+ $ conditions = $ this ->getRequest ()->getParam ('conditionValue ' );
182
51
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
- );
52
+ try {
53
+ $ totals = $ this ->productTotals ->getProductTotals ($ conditions );
54
+ $ response = [
55
+ 'total ' => $ totals ['total ' ],
56
+ 'disabled ' => $ totals ['disabled ' ],
57
+ 'notVisible ' => $ totals ['notVisible ' ],
58
+ 'outOfStock ' => $ totals ['outOfStock ' ],
59
+ ];
192
60
} 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
- );
61
+ $ response = [
62
+ 'total ' => 0 ,
63
+ 'disabled ' => 0 ,
64
+ 'notVisible ' => 0 ,
65
+ 'outOfStock ' => 0 ,
66
+ ];
202
67
}
68
+
69
+ return $ this ->jsonFactory ->create ()->setData ($ response );
203
70
}
204
71
}
0 commit comments