Skip to content

Commit 8501db1

Browse files
authored
Merge pull request #86 from mageplaza/2.4-develop
v4.0.1
2 parents 7fb053f + 37ff02c commit 8501db1

16 files changed

+584
-331
lines changed

Block/AbstractSlider.php

+148-74
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Magento\Catalog\Model\Product\Visibility;
2929
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
3030
use Magento\Catalog\Pricing\Price\FinalPrice;
31+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
3132
use Magento\Framework\App\ActionInterface;
3233
use Magento\Framework\App\Http\Context as HttpContext;
3334
use Magento\Framework\App\ObjectManager;
@@ -38,6 +39,8 @@
3839
use Magento\Framework\Pricing\Render;
3940
use Magento\Framework\Stdlib\DateTime\DateTime;
4041
use Magento\Framework\Url\EncoderInterface;
42+
use Magento\Framework\View\LayoutFactory;
43+
use Magento\GroupedProduct\Model\Product\Type\Grouped;
4144
use Magento\Widget\Block\BlockInterface;
4245
use Mageplaza\Productslider\Helper\Data;
4346
use Mageplaza\Productslider\Model\Config\Source\Additional;
@@ -48,37 +51,50 @@
4851
*/
4952
abstract class AbstractSlider extends AbstractProduct implements BlockInterface, IdentityInterface
5053
{
51-
private $priceCurrency;
52-
5354
/**
5455
* @var DateTime
5556
*/
5657
protected $_date;
57-
5858
/**
5959
* @var Data
6060
*/
6161
protected $_helperData;
62-
6362
/**
6463
* @var CollectionFactory
6564
*/
6665
protected $_productCollectionFactory;
67-
6866
/**
6967
* @var Visibility
7068
*/
7169
protected $_catalogProductVisibility;
72-
7370
/**
7471
* @var HttpContext
7572
*/
7673
protected $httpContext;
77-
7874
/**
7975
* @var EncoderInterface|null
8076
*/
8177
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;
8298

8399
/**
84100
* AbstractSlider constructor.
@@ -90,6 +106,9 @@ abstract class AbstractSlider extends AbstractProduct implements BlockInterface,
90106
* @param Data $helperData
91107
* @param HttpContext $httpContext
92108
* @param EncoderInterface $urlEncoder
109+
* @param Grouped $grouped
110+
* @param Configurable $configurable
111+
* @param LayoutFactory $layoutFactory
93112
* @param array $data
94113
*/
95114
public function __construct(
@@ -100,38 +119,24 @@ public function __construct(
100119
Data $helperData,
101120
HttpContext $httpContext,
102121
EncoderInterface $urlEncoder,
122+
Grouped $grouped,
123+
Configurable $configurable,
124+
LayoutFactory $layoutFactory,
103125
array $data = []
104126
) {
105127
$this->_productCollectionFactory = $productCollectionFactory;
106128
$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;
111136

112137
parent::__construct($context, $data);
113138
}
114139

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-
135140
/**
136141
* Get Key pieces for caching block content
137142
*
@@ -150,41 +155,39 @@ public function getCacheKeyInfo()
150155
}
151156

152157
/**
153-
* @return Data
158+
* {@inheritdoc}
154159
*/
155-
public function getHelperData()
160+
protected function _construct()
156161
{
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');
158170
}
159171

160172
/**
161-
* @return array|mixed
173+
* Get Slider Id
174+
* @return string
162175
*/
163-
public function getDisplayAdditional()
176+
public function getSliderId()
164177
{
165178
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();
173180
}
174181

175-
return $display;
182+
return uniqid('-', false);
176183
}
177184

178185
/**
179-
* @return mixed
186+
* @return Data
180187
*/
181-
private function getPriceCurrency()
188+
public function getHelperData()
182189
{
183-
if ($this->priceCurrency === null) {
184-
$this->priceCurrency = ObjectManager::getInstance()
185-
->get(PriceCurrencyInterface::class);
186-
}
187-
return $this->priceCurrency;
190+
return $this->_helperData;
188191
}
189192

190193
/**
@@ -200,8 +203,8 @@ public function getAddToCartPostParams(Product $product)
200203

201204
return [
202205
'action' => $url,
203-
'data' => [
204-
'product' => $product->getEntityId(),
206+
'data' => [
207+
'product' => $product->getEntityId(),
205208
ActionInterface::PARAM_NAME_URL_ENCODED => $this->urlEncoder->encode($url),
206209
]
207210
];
@@ -216,12 +219,21 @@ public function canShowPrice()
216219
}
217220

218221
/**
219-
* @return bool|\Magento\Framework\View\Element\BlockInterface
220-
* @throws LocalizedException
222+
* @return array|mixed
221223
*/
222-
protected function getPriceRender()
224+
public function getDisplayAdditional()
223225
{
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;
225237
}
226238

227239
/**
@@ -242,18 +254,18 @@ public function getProductPriceHtml(
242254
if (!isset($arguments['zone'])) {
243255
$arguments['zone'] = $renderZone;
244256
}
245-
$arguments['price_id'] = isset($arguments['price_id'])
257+
$arguments['price_id'] = isset($arguments['price_id'])
246258
? $arguments['price_id']
247259
: 'old-price-' . $product->getId() . '-' . $priceType;
248-
$arguments['include_container'] = isset($arguments['include_container'])
260+
$arguments['include_container'] = isset($arguments['include_container'])
249261
? $arguments['include_container']
250262
: true;
251263
$arguments['display_minimal_price'] = isset($arguments['display_minimal_price'])
252264
? $arguments['display_minimal_price']
253265
: true;
254266

255267
/** @var Render $priceRender */
256-
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
268+
$priceRender = $this->getPriceRender();
257269
if (!$priceRender) {
258270
$priceRender = $this->getLayout()->createBlock(
259271
Render::class,
@@ -269,34 +281,42 @@ public function getProductPriceHtml(
269281
);
270282
}
271283

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+
}
272292

273293
/**
274-
* @return bool
294+
* @return mixed
275295
*/
276-
public function canShowReview()
296+
private function getPriceCurrency()
277297
{
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;
279304
}
280305

281306
/**
282307
* @return bool
283308
*/
284-
public function canShowAddToCart()
309+
public function canShowReview()
285310
{
286-
return in_array(Additional::SHOW_CART, $this->getDisplayAdditional(), true);
311+
return in_array(Additional::SHOW_REVIEW, $this->getDisplayAdditional(), true);
287312
}
288313

289314
/**
290-
* Get Slider Id
291-
* @return string
315+
* @return bool
292316
*/
293-
public function getSliderId()
317+
public function canShowAddToCart()
294318
{
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);
300320
}
301321

302322
/**
@@ -341,7 +361,7 @@ public function getDescription()
341361
public function getAllOptions()
342362
{
343363
$sliderOptions = '';
344-
$allConfig = $this->_helperData->getModuleConfig('slider_design');
364+
$allConfig = $this->_helperData->getModuleConfig('slider_design');
345365

346366
foreach ($allConfig as $key => $value) {
347367
if ($key === 'item_slider') {
@@ -445,6 +465,11 @@ public function getIdentities()
445465
return $identities ?: [Product::CACHE_TAG];
446466
}
447467

468+
/**
469+
* @return mixed
470+
*/
471+
abstract public function getProductCollection();
472+
448473
/**
449474
* Get Product Count is displayed
450475
*
@@ -462,4 +487,53 @@ public function getProductsCount()
462487

463488
return 5;
464489
}
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+
}
465539
}

0 commit comments

Comments
 (0)