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,38 +119,24 @@ 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 ;
106
128
$ this ->_catalogProductVisibility = $ catalogProductVisibility ;
107
- $ this ->_date = $ dateTime ;
108
- $ this ->_helperData = $ helperData ;
109
- $ this ->httpContext = $ httpContext ;
110
- $ this ->urlEncoder = $ urlEncoder ;
129
+ $ this ->_date = $ dateTime ;
130
+ $ this ->_helperData = $ helperData ;
131
+ $ this ->httpContext = $ httpContext ;
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,41 +155,39 @@ public function getCacheKeyInfo()
150
155
}
151
156
152
157
/**
153
- * @return Data
158
+ * {@inheritdoc}
154
159
*/
155
- public function getHelperData ()
160
+ protected function _construct ()
156
161
{
157
- return $ this ->_helperData ;
162
+ parent ::_construct ();
163
+
164
+ $ this ->addData ([
165
+ 'cache_lifetime ' => $ this ->getSlider () ? $ this ->getSlider ()->getTimeCache () : 86400 ,
166
+ 'cache_tags ' => [Product::CACHE_TAG ]
167
+ ]);
168
+
169
+ $ this ->setTemplate ('Mageplaza_Productslider::productslider.phtml ' );
158
170
}
159
171
160
172
/**
161
- * @return array|mixed
173
+ * Get Slider Id
174
+ * @return string
162
175
*/
163
- public function getDisplayAdditional ()
176
+ public function getSliderId ()
164
177
{
165
178
if ($ this ->getSlider ()) {
166
- $ display = $ this ->getSlider ()->getDisplayAdditional ();
167
- } else {
168
- $ display = $ this ->_helperData ->getModuleConfig ('general/display_information ' );
169
- }
170
-
171
- if (!is_array ($ display )) {
172
- $ display = explode (', ' , $ display );
179
+ return $ this ->getSlider ()->getSliderId ();
173
180
}
174
181
175
- return $ display ;
182
+ return uniqid ( ' - ' , false ) ;
176
183
}
177
184
178
185
/**
179
- * @return mixed
186
+ * @return Data
180
187
*/
181
- private function getPriceCurrency ()
188
+ public function getHelperData ()
182
189
{
183
- if ($ this ->priceCurrency === null ) {
184
- $ this ->priceCurrency = ObjectManager::getInstance ()
185
- ->get (PriceCurrencyInterface::class);
186
- }
187
- return $ this ->priceCurrency ;
190
+ return $ this ->_helperData ;
188
191
}
189
192
190
193
/**
@@ -200,8 +203,8 @@ public function getAddToCartPostParams(Product $product)
200
203
201
204
return [
202
205
'action ' => $ url ,
203
- 'data ' => [
204
- 'product ' => $ product ->getEntityId (),
206
+ 'data ' => [
207
+ 'product ' => $ product ->getEntityId (),
205
208
ActionInterface::PARAM_NAME_URL_ENCODED => $ this ->urlEncoder ->encode ($ url ),
206
209
]
207
210
];
@@ -216,12 +219,21 @@ public function canShowPrice()
216
219
}
217
220
218
221
/**
219
- * @return bool|\Magento\Framework\View\Element\BlockInterface
220
- * @throws LocalizedException
222
+ * @return array|mixed
221
223
*/
222
- protected function getPriceRender ()
224
+ public function getDisplayAdditional ()
223
225
{
224
- return $ this ->getLayout ()->getBlock ('product.price.render.default ' );
226
+ if ($ this ->getSlider ()) {
227
+ $ display = $ this ->getSlider ()->getDisplayAdditional ();
228
+ } else {
229
+ $ display = $ this ->_helperData ->getModuleConfig ('general/display_information ' );
230
+ }
231
+
232
+ if (!is_array ($ display )) {
233
+ $ display = explode (', ' , $ display );
234
+ }
235
+
236
+ return $ display ;
225
237
}
226
238
227
239
/**
@@ -242,18 +254,18 @@ public function getProductPriceHtml(
242
254
if (!isset ($ arguments ['zone ' ])) {
243
255
$ arguments ['zone ' ] = $ renderZone ;
244
256
}
245
- $ arguments ['price_id ' ] = isset ($ arguments ['price_id ' ])
257
+ $ arguments ['price_id ' ] = isset ($ arguments ['price_id ' ])
246
258
? $ arguments ['price_id ' ]
247
259
: 'old-price- ' . $ product ->getId () . '- ' . $ priceType ;
248
- $ arguments ['include_container ' ] = isset ($ arguments ['include_container ' ])
260
+ $ arguments ['include_container ' ] = isset ($ arguments ['include_container ' ])
249
261
? $ arguments ['include_container ' ]
250
262
: true ;
251
263
$ arguments ['display_minimal_price ' ] = isset ($ arguments ['display_minimal_price ' ])
252
264
? $ arguments ['display_minimal_price ' ]
253
265
: true ;
254
266
255
267
/** @var Render $priceRender */
256
- $ priceRender = $ this ->getLayout ()-> getBlock ( ' product.price.render.default ' );
268
+ $ priceRender = $ this ->getPriceRender ( );
257
269
if (!$ priceRender ) {
258
270
$ priceRender = $ this ->getLayout ()->createBlock (
259
271
Render::class,
@@ -269,34 +281,42 @@ public function getProductPriceHtml(
269
281
);
270
282
}
271
283
284
+ /**
285
+ * @return bool|\Magento\Framework\View\Element\BlockInterface
286
+ * @throws LocalizedException
287
+ */
288
+ protected function getPriceRender ()
289
+ {
290
+ return $ this ->getLayout ()->getBlock ('product.price.render.default ' );
291
+ }
272
292
273
293
/**
274
- * @return bool
294
+ * @return mixed
275
295
*/
276
- public function canShowReview ()
296
+ private function getPriceCurrency ()
277
297
{
278
- return in_array (Additional::SHOW_REVIEW , $ this ->getDisplayAdditional (), true );
298
+ if ($ this ->priceCurrency === null ) {
299
+ $ this ->priceCurrency = ObjectManager::getInstance ()
300
+ ->get (PriceCurrencyInterface::class);
301
+ }
302
+
303
+ return $ this ->priceCurrency ;
279
304
}
280
305
281
306
/**
282
307
* @return bool
283
308
*/
284
- public function canShowAddToCart ()
309
+ public function canShowReview ()
285
310
{
286
- return in_array (Additional::SHOW_CART , $ this ->getDisplayAdditional (), true );
311
+ return in_array (Additional::SHOW_REVIEW , $ this ->getDisplayAdditional (), true );
287
312
}
288
313
289
314
/**
290
- * Get Slider Id
291
- * @return string
315
+ * @return bool
292
316
*/
293
- public function getSliderId ()
317
+ public function canShowAddToCart ()
294
318
{
295
- if ($ this ->getSlider ()) {
296
- return $ this ->getSlider ()->getSliderId ();
297
- }
298
-
299
- return uniqid ('- ' , false );
319
+ return in_array (Additional::SHOW_CART , $ this ->getDisplayAdditional (), true );
300
320
}
301
321
302
322
/**
@@ -341,7 +361,7 @@ public function getDescription()
341
361
public function getAllOptions ()
342
362
{
343
363
$ sliderOptions = '' ;
344
- $ allConfig = $ this ->_helperData ->getModuleConfig ('slider_design ' );
364
+ $ allConfig = $ this ->_helperData ->getModuleConfig ('slider_design ' );
345
365
346
366
foreach ($ allConfig as $ key => $ value ) {
347
367
if ($ key === 'item_slider ' ) {
@@ -445,6 +465,11 @@ public function getIdentities()
445
465
return $ identities ?: [Product::CACHE_TAG ];
446
466
}
447
467
468
+ /**
469
+ * @return mixed
470
+ */
471
+ abstract public function getProductCollection ();
472
+
448
473
/**
449
474
* Get Product Count is displayed
450
475
*
@@ -462,4 +487,53 @@ public function getProductsCount()
462
487
463
488
return 5 ;
464
489
}
490
+
491
+ /**
492
+ * @param $collection
493
+ *
494
+ * @return array
495
+ */
496
+ public function getProductParentIds ($ collection )
497
+ {
498
+ $ productIds = [];
499
+
500
+ foreach ($ collection as $ product ) {
501
+ if (isset ($ product ->getData ()['entity_id ' ])) {
502
+ $ productId = $ product ->getData ()['entity_id ' ];
503
+ } else {
504
+ $ productId = $ product ->getProductId ();
505
+ }
506
+
507
+ $ parentIdsGroup = $ this ->grouped ->getParentIdsByChild ($ productId );
508
+ $ parentIdsConfig = $ this ->configurable ->getParentIdsByChild ($ productId );
509
+
510
+ if (!empty ($ parentIdsGroup )) {
511
+ $ productIds [] = $ parentIdsGroup ;
512
+ } elseif (!empty ($ parentIdsConfig )) {
513
+ $ productIds [] = $ parentIdsConfig [0 ];
514
+ } else {
515
+ $ productIds [] = $ productId ;
516
+ }
517
+ }
518
+
519
+ return $ productIds ;
520
+ }
521
+
522
+ /**
523
+ * @return bool|\Magento\Framework\View\Element\BlockInterface|\Magento\Framework\View\Element\RendererList
524
+ * @throws LocalizedException
525
+ */
526
+ protected function getDetailsRendererList ()
527
+ {
528
+ if (empty ($ this ->rendererListBlock )) {
529
+ $ layout = $ this ->layoutFactory ->create (['cacheable ' => false ]);
530
+ $ layout ->getUpdate ()->addHandle ('catalog_widget_product_list ' )->load ();
531
+ $ layout ->generateXml ();
532
+ $ layout ->generateElements ();
533
+
534
+ $ this ->rendererListBlock = $ layout ->getBlock ('category.product.type.widget.details.renderers ' );
535
+ }
536
+
537
+ return $ this ->rendererListBlock ;
538
+ }
465
539
}
0 commit comments