28
28
use Magento \Catalog \Model \Product \Visibility ;
29
29
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
30
30
use Magento \Catalog \Pricing \Price \FinalPrice ;
31
+ use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
31
32
use Magento \Framework \App \ActionInterface ;
32
33
use Magento \Framework \App \Http \Context as HttpContext ;
33
34
use Magento \Framework \App \ObjectManager ;
38
39
use Magento \Framework \Pricing \Render ;
39
40
use Magento \Framework \Stdlib \DateTime \DateTime ;
40
41
use Magento \Framework \Url \EncoderInterface ;
42
+ use Magento \Framework \View \LayoutFactory ;
43
+ use Magento \GroupedProduct \Model \Product \Type \Grouped ;
41
44
use Magento \Widget \Block \BlockInterface ;
42
45
use Mageplaza \Productslider \Helper \Data ;
43
46
use Mageplaza \Productslider \Model \Config \Source \Additional ;
48
51
*/
49
52
abstract class AbstractSlider extends AbstractProduct implements BlockInterface, IdentityInterface
50
53
{
51
- private $ priceCurrency ;
52
-
53
54
/**
54
55
* @var DateTime
55
56
*/
56
57
protected $ _date ;
57
-
58
58
/**
59
59
* @var Data
60
60
*/
61
61
protected $ _helperData ;
62
-
63
62
/**
64
63
* @var CollectionFactory
65
64
*/
66
65
protected $ _productCollectionFactory ;
67
-
68
66
/**
69
67
* @var Visibility
70
68
*/
71
69
protected $ _catalogProductVisibility ;
72
-
73
70
/**
74
71
* @var HttpContext
75
72
*/
76
73
protected $ httpContext ;
77
-
78
74
/**
79
75
* @var EncoderInterface|null
80
76
*/
81
77
protected $ urlEncoder ;
78
+ /**
79
+ * @var Grouped
80
+ */
81
+ protected $ grouped ;
82
+ /**
83
+ * @var Configurable
84
+ */
85
+ protected $ configurable ;
86
+ /**
87
+ * @var
88
+ */
89
+ protected $ rendererListBlock ;
90
+ /**
91
+ * @var
92
+ */
93
+ private $ priceCurrency ;
94
+ /**
95
+ * @var LayoutFactory
96
+ */
97
+ private $ layoutFactory ;
82
98
83
99
/**
84
100
* AbstractSlider constructor.
@@ -90,6 +106,9 @@ abstract class AbstractSlider extends AbstractProduct implements BlockInterface,
90
106
* @param Data $helperData
91
107
* @param HttpContext $httpContext
92
108
* @param EncoderInterface $urlEncoder
109
+ * @param Grouped $grouped
110
+ * @param Configurable $configurable
111
+ * @param LayoutFactory $layoutFactory
93
112
* @param array $data
94
113
*/
95
114
public function __construct (
@@ -100,6 +119,9 @@ public function __construct(
100
119
Data $ helperData ,
101
120
HttpContext $ httpContext ,
102
121
EncoderInterface $ urlEncoder ,
122
+ Grouped $ grouped ,
123
+ Configurable $ configurable ,
124
+ LayoutFactory $ layoutFactory ,
103
125
array $ data = []
104
126
) {
105
127
$ this ->_productCollectionFactory = $ productCollectionFactory ;
@@ -108,30 +130,13 @@ public function __construct(
108
130
$ this ->_helperData = $ helperData ;
109
131
$ this ->httpContext = $ httpContext ;
110
132
$ this ->urlEncoder = $ urlEncoder ;
133
+ $ this ->grouped = $ grouped ;
134
+ $ this ->configurable = $ configurable ;
135
+ $ this ->layoutFactory = $ layoutFactory ;
111
136
112
137
parent ::__construct ($ context , $ data );
113
138
}
114
139
115
- /**
116
- * {@inheritdoc}
117
- */
118
- protected function _construct ()
119
- {
120
- parent ::_construct ();
121
-
122
- $ this ->addData ([
123
- 'cache_lifetime ' => $ this ->getSlider () ? $ this ->getSlider ()->getTimeCache () : 86400 ,
124
- 'cache_tags ' => [Product::CACHE_TAG ]
125
- ]);
126
-
127
- $ this ->setTemplate ('Mageplaza_Productslider::productslider.phtml ' );
128
- }
129
-
130
- /**
131
- * @return mixed
132
- */
133
- abstract public function getProductCollection ();
134
-
135
140
/**
136
141
* Get Key pieces for caching block content
137
142
*
@@ -150,34 +155,31 @@ public function getCacheKeyInfo()
150
155
}
151
156
152
157
/**
153
- * @return array|mixed
158
+ * {@inheritdoc}
154
159
*/
155
- public function getDisplayAdditional ()
160
+ protected function _construct ()
156
161
{
157
- if ($ this ->getSlider ()) {
158
- $ display = $ this ->getSlider ()->getDisplayAdditional ();
159
- } else {
160
- $ display = $ this ->_helperData ->getModuleConfig ('general/display_information ' );
161
- }
162
+ parent ::_construct ();
162
163
163
- if (!is_array ($ display )) {
164
- $ display = explode (', ' , $ display );
165
- }
164
+ $ this ->addData ([
165
+ 'cache_lifetime ' => $ this ->getSlider () ? $ this ->getSlider ()->getTimeCache () : 86400 ,
166
+ 'cache_tags ' => [Product::CACHE_TAG ]
167
+ ]);
166
168
167
- return $ display ;
169
+ $ this -> setTemplate ( ' Mageplaza_Productslider::productslider.phtml ' ) ;
168
170
}
169
171
170
172
/**
171
- * @return mixed
173
+ * Get Slider Id
174
+ * @return string
172
175
*/
173
- private function getPriceCurrency ()
176
+ public function getSliderId ()
174
177
{
175
- if ($ this ->priceCurrency === null ) {
176
- $ this ->priceCurrency = ObjectManager::getInstance ()
177
- ->get (PriceCurrencyInterface::class);
178
+ if ($ this ->getSlider ()) {
179
+ return $ this ->getSlider ()->getSliderId ();
178
180
}
179
181
180
- return $ this -> priceCurrency ;
182
+ return uniqid ( ' - ' , false ) ;
181
183
}
182
184
183
185
/**
@@ -209,12 +211,21 @@ public function canShowPrice()
209
211
}
210
212
211
213
/**
212
- * @return bool|\Magento\Framework\View\Element\BlockInterface
213
- * @throws LocalizedException
214
+ * @return array|mixed
214
215
*/
215
- protected function getPriceRender ()
216
+ public function getDisplayAdditional ()
216
217
{
217
- return $ this ->getLayout ()->getBlock ('product.price.render.default ' );
218
+ if ($ this ->getSlider ()) {
219
+ $ display = $ this ->getSlider ()->getDisplayAdditional ();
220
+ } else {
221
+ $ display = $ this ->_helperData ->getModuleConfig ('general/display_information ' );
222
+ }
223
+
224
+ if (!is_array ($ display )) {
225
+ $ display = explode (', ' , $ display );
226
+ }
227
+
228
+ return $ display ;
218
229
}
219
230
220
231
/**
@@ -246,7 +257,7 @@ public function getProductPriceHtml(
246
257
: true ;
247
258
248
259
/** @var Render $priceRender */
249
- $ priceRender = $ this ->getLayout ()-> getBlock ( ' product.price.render.default ' );
260
+ $ priceRender = $ this ->getPriceRender ( );
250
261
if (!$ priceRender ) {
251
262
$ priceRender = $ this ->getLayout ()->createBlock (
252
263
Render::class,
@@ -263,32 +274,41 @@ public function getProductPriceHtml(
263
274
}
264
275
265
276
/**
266
- * @return bool
277
+ * @return bool|\Magento\Framework\View\Element\BlockInterface
278
+ * @throws LocalizedException
267
279
*/
268
- public function canShowReview ()
280
+ protected function getPriceRender ()
269
281
{
270
- return in_array (Additional:: SHOW_REVIEW , $ this ->getDisplayAdditional (), true );
282
+ return $ this ->getLayout ()-> getBlock ( ' product.price.render.default ' );
271
283
}
272
284
273
285
/**
274
- * @return bool
286
+ * @return mixed
275
287
*/
276
- public function canShowAddToCart ()
288
+ private function getPriceCurrency ()
277
289
{
278
- return in_array (Additional::SHOW_CART , $ this ->getDisplayAdditional (), true );
290
+ if ($ this ->priceCurrency === null ) {
291
+ $ this ->priceCurrency = ObjectManager::getInstance ()
292
+ ->get (PriceCurrencyInterface::class);
293
+ }
294
+
295
+ return $ this ->priceCurrency ;
279
296
}
280
297
281
298
/**
282
- * Get Slider Id
283
- * @return string
299
+ * @return bool
284
300
*/
285
- public function getSliderId ()
301
+ public function canShowReview ()
286
302
{
287
- if ($ this ->getSlider ()) {
288
- return $ this ->getSlider ()->getSliderId ();
289
- }
303
+ return in_array (Additional::SHOW_REVIEW , $ this ->getDisplayAdditional (), true );
304
+ }
290
305
291
- return uniqid ('- ' , false );
306
+ /**
307
+ * @return bool
308
+ */
309
+ public function canShowAddToCart ()
310
+ {
311
+ return in_array (Additional::SHOW_CART , $ this ->getDisplayAdditional (), true );
292
312
}
293
313
294
314
/**
@@ -437,6 +457,11 @@ public function getIdentities()
437
457
return $ identities ?: [Product::CACHE_TAG ];
438
458
}
439
459
460
+ /**
461
+ * @return mixed
462
+ */
463
+ abstract public function getProductCollection ();
464
+
440
465
/**
441
466
* Get Product Count is displayed
442
467
*
@@ -454,4 +479,53 @@ public function getProductsCount()
454
479
455
480
return 5 ;
456
481
}
482
+
483
+ /**
484
+ * @param $collection
485
+ *
486
+ * @return array
487
+ */
488
+ public function getProductParentIds ($ collection )
489
+ {
490
+ $ productIds = [];
491
+
492
+ foreach ($ collection as $ product ) {
493
+ if (isset ($ product ->getData ()['entity_id ' ])) {
494
+ $ productId = $ product ->getData ()['entity_id ' ];
495
+ } else {
496
+ $ productId = $ product ->getProductId ();
497
+ }
498
+
499
+ $ parentIdsGroup = $ this ->grouped ->getParentIdsByChild ($ productId );
500
+ $ parentIdsConfig = $ this ->configurable ->getParentIdsByChild ($ productId );
501
+
502
+ if (!empty ($ parentIdsGroup )) {
503
+ $ productIds [] = $ parentIdsGroup ;
504
+ } elseif (!empty ($ parentIdsConfig )) {
505
+ $ productIds [] = $ parentIdsConfig [0 ];
506
+ } else {
507
+ $ productIds [] = $ productId ;
508
+ }
509
+ }
510
+
511
+ return $ productIds ;
512
+ }
513
+
514
+ /**
515
+ * @return bool|\Magento\Framework\View\Element\BlockInterface|\Magento\Framework\View\Element\RendererList
516
+ * @throws LocalizedException
517
+ */
518
+ protected function getDetailsRendererList ()
519
+ {
520
+ if (empty ($ this ->rendererListBlock )) {
521
+ $ layout = $ this ->layoutFactory ->create (['cacheable ' => false ]);
522
+ $ layout ->getUpdate ()->addHandle ('catalog_widget_product_list ' )->load ();
523
+ $ layout ->generateXml ();
524
+ $ layout ->generateElements ();
525
+
526
+ $ this ->rendererListBlock = $ layout ->getBlock ('category.product.type.widget.details.renderers ' );
527
+ }
528
+
529
+ return $ this ->rendererListBlock ;
530
+ }
457
531
}
0 commit comments