6
6
7
7
namespace Magento \CatalogWidget \Block \Product ;
8
8
9
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
10
+ use Magento \Catalog \Block \Product \AbstractProduct ;
11
+ use Magento \Catalog \Block \Product \Widget \Html \Pager ;
9
12
use Magento \Catalog \Model \Product ;
10
- use Magento \Framework \App \ObjectManager ;
13
+ use Magento \Catalog \Model \Product \Visibility ;
14
+ use Magento \Catalog \Model \ResourceModel \Product \Collection ;
15
+ use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
16
+ use Magento \Catalog \Pricing \Price \FinalPrice ;
17
+ use Magento \CatalogWidget \Model \Rule ;
11
18
use Magento \Framework \App \ActionInterface ;
19
+ use Magento \Framework \App \Http \Context ;
20
+ use Magento \Framework \App \ObjectManager ;
12
21
use Magento \Framework \DataObject \IdentityInterface ;
22
+ use Magento \Framework \Exception \LocalizedException ;
23
+ use Magento \Framework \Exception \NoSuchEntityException ;
13
24
use Magento \Framework \Pricing \PriceCurrencyInterface ;
14
25
use Magento \Framework \Serialize \Serializer \Json ;
26
+ use Magento \Framework \Url \EncoderInterface ;
15
27
use Magento \Framework \View \LayoutFactory ;
28
+ use Magento \Framework \View \LayoutInterface ;
29
+ use Magento \Rule \Model \Condition \Combine ;
30
+ use Magento \Rule \Model \Condition \Sql \Builder ;
16
31
use Magento \Widget \Block \BlockInterface ;
17
- use Magento \Framework \ Url \ EncoderInterface ;
32
+ use Magento \Widget \ Helper \ Conditions ;
18
33
19
34
/**
20
35
* Catalog Products List widget block
21
36
*
22
37
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
38
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
24
39
*/
25
- class ProductsList extends \ Magento \ Catalog \ Block \ Product \ AbstractProduct implements BlockInterface, IdentityInterface
40
+ class ProductsList extends AbstractProduct implements BlockInterface, IdentityInterface
26
41
{
27
42
/**
28
43
* Default value for products count that will be shown
@@ -32,7 +47,7 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
32
47
/**
33
48
* Name of request parameter for page number value
34
49
*
35
- * @deprecated
50
+ * @deprecated @see $this->getData('page_var_name')
36
51
*/
37
52
const PAGE_VAR_NAME = 'np ' ;
38
53
@@ -49,41 +64,41 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
49
64
/**
50
65
* Instance of pager block
51
66
*
52
- * @var \Magento\Catalog\Block\Product\Widget\Html\ Pager
67
+ * @var Pager
53
68
*/
54
69
protected $ pager ;
55
70
56
71
/**
57
- * @var \Magento\Framework\App\Http\ Context
72
+ * @var Context
58
73
*/
59
74
protected $ httpContext ;
60
75
61
76
/**
62
77
* Catalog product visibility
63
78
*
64
- * @var \Magento\Catalog\Model\Product\ Visibility
79
+ * @var Visibility
65
80
*/
66
81
protected $ catalogProductVisibility ;
67
82
68
83
/**
69
84
* Product collection factory
70
85
*
71
- * @var \Magento\Catalog\Model\ResourceModel\Product\ CollectionFactory
86
+ * @var CollectionFactory
72
87
*/
73
88
protected $ productCollectionFactory ;
74
89
75
90
/**
76
- * @var \Magento\Rule\Model\Condition\Sql\ Builder
91
+ * @var Builder
77
92
*/
78
93
protected $ sqlBuilder ;
79
94
80
95
/**
81
- * @var \Magento\CatalogWidget\Model\ Rule
96
+ * @var Rule
82
97
*/
83
98
protected $ rule ;
84
99
85
100
/**
86
- * @var \Magento\Widget\Helper\ Conditions
101
+ * @var Conditions
87
102
*/
88
103
protected $ conditionsHelper ;
89
104
@@ -105,7 +120,7 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
105
120
private $ layoutFactory ;
106
121
107
122
/**
108
- * @var \Magento\Framework\Url\ EncoderInterface|null
123
+ * @var EncoderInterface|null
109
124
*/
110
125
private $ urlEncoder ;
111
126
@@ -114,29 +129,36 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
114
129
*/
115
130
private $ rendererListBlock ;
116
131
132
+ /**
133
+ * @var CategoryRepositoryInterface
134
+ */
135
+ private $ categoryRepository ;
136
+
117
137
/**
118
138
* @param \Magento\Catalog\Block\Product\Context $context
119
- * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
120
- * @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
121
- * @param \Magento\Framework\App\Http\Context $httpContext
122
- * @param \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder
123
- * @param \Magento\CatalogWidget\Model\Rule $rule
124
- * @param \Magento\Widget\Helper\Conditions $conditionsHelper
139
+ * @param CollectionFactory $productCollectionFactory
140
+ * @param Visibility $catalogProductVisibility
141
+ * @param Context $httpContext
142
+ * @param Builder $sqlBuilder
143
+ * @param Rule $rule
144
+ * @param Conditions $conditionsHelper
145
+ * @param CategoryRepositoryInterface $categoryRepository
125
146
* @param array $data
126
147
* @param Json|null $json
127
148
* @param LayoutFactory|null $layoutFactory
128
- * @param \Magento\Framework\Url\ EncoderInterface|null $urlEncoder
149
+ * @param EncoderInterface|null $urlEncoder
129
150
*
130
151
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131
152
*/
132
153
public function __construct (
133
154
\Magento \Catalog \Block \Product \Context $ context ,
134
- \Magento \Catalog \Model \ResourceModel \Product \CollectionFactory $ productCollectionFactory ,
135
- \Magento \Catalog \Model \Product \Visibility $ catalogProductVisibility ,
136
- \Magento \Framework \App \Http \Context $ httpContext ,
137
- \Magento \Rule \Model \Condition \Sql \Builder $ sqlBuilder ,
138
- \Magento \CatalogWidget \Model \Rule $ rule ,
139
- \Magento \Widget \Helper \Conditions $ conditionsHelper ,
155
+ CollectionFactory $ productCollectionFactory ,
156
+ Visibility $ catalogProductVisibility ,
157
+ Context $ httpContext ,
158
+ Builder $ sqlBuilder ,
159
+ Rule $ rule ,
160
+ Conditions $ conditionsHelper ,
161
+ CategoryRepositoryInterface $ categoryRepository ,
140
162
array $ data = [],
141
163
Json $ json = null ,
142
164
LayoutFactory $ layoutFactory = null ,
@@ -151,6 +173,7 @@ public function __construct(
151
173
$ this ->json = $ json ?: ObjectManager::getInstance ()->get (Json::class);
152
174
$ this ->layoutFactory = $ layoutFactory ?: ObjectManager::getInstance ()->get (LayoutFactory::class);
153
175
$ this ->urlEncoder = $ urlEncoder ?: ObjectManager::getInstance ()->get (EncoderInterface::class);
176
+ $ this ->categoryRepository = $ categoryRepository ;
154
177
parent ::__construct (
155
178
$ context ,
156
179
$ data
@@ -171,17 +194,22 @@ protected function _construct()
171
194
->addColumnCountLayoutDepend ('2columns-right ' , 4 )
172
195
->addColumnCountLayoutDepend ('3columns ' , 3 );
173
196
174
- $ this ->addData ([
175
- 'cache_lifetime ' => 86400 ,
176
- 'cache_tags ' => [\Magento \Catalog \Model \Product::CACHE_TAG ,
177
- ], ]);
197
+ $ this ->addData (
198
+ [
199
+ 'cache_lifetime ' => 86400 ,
200
+ 'cache_tags ' => [
201
+ Product::CACHE_TAG ,
202
+ ],
203
+ ]
204
+ );
178
205
}
179
206
180
207
/**
181
208
* Get key pieces for caching block content
182
209
*
183
210
* @return array
184
211
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
212
+ * @throws NoSuchEntityException
185
213
*/
186
214
public function getCacheKeyInfo ()
187
215
{
@@ -195,7 +223,7 @@ public function getCacheKeyInfo()
195
223
$ this ->_storeManager ->getStore ()->getId (),
196
224
$ this ->_design ->getDesignTheme ()->getId (),
197
225
$ this ->httpContext ->getValue (\Magento \Customer \Model \Context::CONTEXT_GROUP ),
198
- (int ) $ this ->getRequest ()->getParam ($ this ->getData ('page_var_name ' ), 1 ),
226
+ (int )$ this ->getRequest ()->getParam ($ this ->getData ('page_var_name ' ), 1 ),
199
227
$ this ->getProductsPerPage (),
200
228
$ this ->getProductsCount (),
201
229
$ conditions ,
@@ -210,7 +238,7 @@ public function getCacheKeyInfo()
210
238
* @SuppressWarnings(PHPMD.NPathComplexity)
211
239
*/
212
240
public function getProductPriceHtml (
213
- \ Magento \ Catalog \ Model \ Product $ product ,
241
+ Product $ product ,
214
242
$ priceType = null ,
215
243
$ renderZone = \Magento \Framework \Pricing \Render::ZONE_ITEM_LIST ,
216
244
array $ arguments = []
@@ -239,7 +267,7 @@ public function getProductPriceHtml(
239
267
}
240
268
241
269
$ price = $ priceRender ->render (
242
- \ Magento \ Catalog \ Pricing \ Price \ FinalPrice::PRICE_CODE ,
270
+ FinalPrice::PRICE_CODE ,
243
271
$ product ,
244
272
$ arguments
245
273
);
@@ -253,7 +281,7 @@ public function getProductPriceHtml(
253
281
protected function getDetailsRendererList ()
254
282
{
255
283
if (empty ($ this ->rendererListBlock )) {
256
- /** @var $layout \Magento\Framework\View\ LayoutInterface */
284
+ /** @var $layout LayoutInterface */
257
285
$ layout = $ this ->layoutFactory ->create (['cacheable ' => false ]);
258
286
$ layout ->getUpdate ()->addHandle ('catalog_widget_product_list ' )->load ();
259
287
$ layout ->generateXml ();
@@ -294,12 +322,13 @@ protected function _beforeToHtml()
294
322
/**
295
323
* Prepare and return product collection
296
324
*
297
- * @return \Magento\Catalog\Model\ResourceModel\Product\ Collection
325
+ * @return Collection
298
326
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
327
+ * @throws LocalizedException
299
328
*/
300
329
public function createCollection ()
301
330
{
302
- /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\ Collection */
331
+ /** @var $collection Collection */
303
332
$ collection = $ this ->productCollectionFactory ->create ();
304
333
305
334
if ($ this ->getData ('store_id ' ) !== null ) {
@@ -327,10 +356,38 @@ public function createCollection()
327
356
return $ collection ;
328
357
}
329
358
359
+ /**
360
+ * Update conditions if the category is an anchor category
361
+ *
362
+ * @param array $condition
363
+ * @return array
364
+ */
365
+ private function updateAnchorCategoryConditions (array $ condition ): array
366
+ {
367
+ if (array_key_exists ('value ' , $ condition )) {
368
+ $ categoryId = $ condition ['value ' ];
369
+
370
+ try {
371
+ $ category = $ this ->categoryRepository ->get ($ categoryId , $ this ->_storeManager ->getStore ()->getId ());
372
+ } catch (NoSuchEntityException $ e ) {
373
+ return $ condition ;
374
+ }
375
+
376
+ $ children = $ category ->getIsAnchor () ? $ category ->getChildren (true ) : [];
377
+ if ($ children ) {
378
+ $ children = explode (', ' , $ children );
379
+ $ condition ['operator ' ] = "() " ;
380
+ $ condition ['value ' ] = array_merge ([$ categoryId ], $ children );
381
+ }
382
+ }
383
+
384
+ return $ condition ;
385
+ }
386
+
330
387
/**
331
388
* Get conditions
332
389
*
333
- * @return \Magento\Rule\Model\Condition\ Combine
390
+ * @return Combine
334
391
*/
335
392
protected function getConditions ()
336
393
{
@@ -343,10 +400,14 @@ protected function getConditions()
343
400
}
344
401
345
402
foreach ($ conditions as $ key => $ condition ) {
346
- if (!empty ($ condition ['attribute ' ])
347
- && in_array ($ condition ['attribute ' ], ['special_from_date ' , 'special_to_date ' ])
348
- ) {
349
- $ conditions [$ key ]['value ' ] = date ('Y-m-d H:i:s ' , strtotime ($ condition ['value ' ]));
403
+ if (!empty ($ condition ['attribute ' ])) {
404
+ if (in_array ($ condition ['attribute ' ], ['special_from_date ' , 'special_to_date ' ])) {
405
+ $ conditions [$ key ]['value ' ] = date ('Y-m-d H:i:s ' , strtotime ($ condition ['value ' ]));
406
+ }
407
+
408
+ if ($ condition ['attribute ' ] == 'category_ids ' ) {
409
+ $ conditions [$ key ] = $ this ->updateAnchorCategoryConditions ($ condition );
410
+ }
350
411
}
351
412
}
352
413
@@ -412,13 +473,14 @@ protected function getPageSize()
412
473
* Render pagination HTML
413
474
*
414
475
* @return string
476
+ * @throws LocalizedException
415
477
*/
416
478
public function getPagerHtml ()
417
479
{
418
480
if ($ this ->showPager () && $ this ->getProductCollection ()->getSize () > $ this ->getProductsPerPage ()) {
419
481
if (!$ this ->pager ) {
420
482
$ this ->pager = $ this ->getLayout ()->createBlock (
421
- \ Magento \ Catalog \ Block \ Product \ Widget \ Html \ Pager::class,
483
+ Pager::class,
422
484
$ this ->getWidgetPagerBlockName ()
423
485
);
424
486
@@ -448,12 +510,12 @@ public function getIdentities()
448
510
if ($ this ->getProductCollection ()) {
449
511
foreach ($ this ->getProductCollection () as $ product ) {
450
512
if ($ product instanceof IdentityInterface) {
451
- $ identities = array_merge ( $ identities , $ product ->getIdentities () );
513
+ $ identities += $ product ->getIdentities ();
452
514
}
453
515
}
454
516
}
455
517
456
- return $ identities ?: [\ Magento \ Catalog \ Model \ Product::CACHE_TAG ];
518
+ return $ identities ?: [Product::CACHE_TAG ];
457
519
}
458
520
459
521
/**
@@ -475,7 +537,7 @@ public function getTitle()
475
537
private function getPriceCurrency ()
476
538
{
477
539
if ($ this ->priceCurrency === null ) {
478
- $ this ->priceCurrency = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
540
+ $ this ->priceCurrency = ObjectManager::getInstance ()
479
541
->get (PriceCurrencyInterface::class);
480
542
}
481
543
return $ this ->priceCurrency ;
0 commit comments