Skip to content

Commit

Permalink
Merge pull request #86 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
v4.0.1
  • Loading branch information
DHT-Uri authored Jun 2, 2021
2 parents 7fb053f + 37ff02c commit 8501db1
Show file tree
Hide file tree
Showing 16 changed files with 584 additions and 331 deletions.
222 changes: 148 additions & 74 deletions Block/AbstractSlider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\App\ObjectManager;
Expand All @@ -38,6 +39,8 @@
use Magento\Framework\Pricing\Render;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\View\LayoutFactory;
use Magento\GroupedProduct\Model\Product\Type\Grouped;
use Magento\Widget\Block\BlockInterface;
use Mageplaza\Productslider\Helper\Data;
use Mageplaza\Productslider\Model\Config\Source\Additional;
Expand All @@ -48,37 +51,50 @@
*/
abstract class AbstractSlider extends AbstractProduct implements BlockInterface, IdentityInterface
{
private $priceCurrency;

/**
* @var DateTime
*/
protected $_date;

/**
* @var Data
*/
protected $_helperData;

/**
* @var CollectionFactory
*/
protected $_productCollectionFactory;

/**
* @var Visibility
*/
protected $_catalogProductVisibility;

/**
* @var HttpContext
*/
protected $httpContext;

/**
* @var EncoderInterface|null
*/
protected $urlEncoder;
/**
* @var Grouped
*/
protected $grouped;
/**
* @var Configurable
*/
protected $configurable;
/**
* @var
*/
protected $rendererListBlock;
/**
* @var
*/
private $priceCurrency;
/**
* @var LayoutFactory
*/
private $layoutFactory;

/**
* AbstractSlider constructor.
Expand All @@ -90,6 +106,9 @@ abstract class AbstractSlider extends AbstractProduct implements BlockInterface,
* @param Data $helperData
* @param HttpContext $httpContext
* @param EncoderInterface $urlEncoder
* @param Grouped $grouped
* @param Configurable $configurable
* @param LayoutFactory $layoutFactory
* @param array $data
*/
public function __construct(
Expand All @@ -100,38 +119,24 @@ public function __construct(
Data $helperData,
HttpContext $httpContext,
EncoderInterface $urlEncoder,
Grouped $grouped,
Configurable $configurable,
LayoutFactory $layoutFactory,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
$this->_catalogProductVisibility = $catalogProductVisibility;
$this->_date = $dateTime;
$this->_helperData = $helperData;
$this->httpContext = $httpContext;
$this->urlEncoder = $urlEncoder;
$this->_date = $dateTime;
$this->_helperData = $helperData;
$this->httpContext = $httpContext;
$this->urlEncoder = $urlEncoder;
$this->grouped = $grouped;
$this->configurable = $configurable;
$this->layoutFactory = $layoutFactory;

parent::__construct($context, $data);
}

/**
* {@inheritdoc}
*/
protected function _construct()
{
parent::_construct();

$this->addData([
'cache_lifetime' => $this->getSlider() ? $this->getSlider()->getTimeCache() : 86400,
'cache_tags' => [Product::CACHE_TAG]
]);

$this->setTemplate('Mageplaza_Productslider::productslider.phtml');
}

/**
* @return mixed
*/
abstract public function getProductCollection();

/**
* Get Key pieces for caching block content
*
Expand All @@ -150,41 +155,39 @@ public function getCacheKeyInfo()
}

/**
* @return Data
* {@inheritdoc}
*/
public function getHelperData()
protected function _construct()
{
return $this->_helperData;
parent::_construct();

$this->addData([
'cache_lifetime' => $this->getSlider() ? $this->getSlider()->getTimeCache() : 86400,
'cache_tags' => [Product::CACHE_TAG]
]);

$this->setTemplate('Mageplaza_Productslider::productslider.phtml');
}

/**
* @return array|mixed
* Get Slider Id
* @return string
*/
public function getDisplayAdditional()
public function getSliderId()
{
if ($this->getSlider()) {
$display = $this->getSlider()->getDisplayAdditional();
} else {
$display = $this->_helperData->getModuleConfig('general/display_information');
}

if (!is_array($display)) {
$display = explode(',', $display);
return $this->getSlider()->getSliderId();
}

return $display;
return uniqid('-', false);
}

/**
* @return mixed
* @return Data
*/
private function getPriceCurrency()
public function getHelperData()
{
if ($this->priceCurrency === null) {
$this->priceCurrency = ObjectManager::getInstance()
->get(PriceCurrencyInterface::class);
}
return $this->priceCurrency;
return $this->_helperData;
}

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

return [
'action' => $url,
'data' => [
'product' => $product->getEntityId(),
'data' => [
'product' => $product->getEntityId(),
ActionInterface::PARAM_NAME_URL_ENCODED => $this->urlEncoder->encode($url),
]
];
Expand All @@ -216,12 +219,21 @@ public function canShowPrice()
}

/**
* @return bool|\Magento\Framework\View\Element\BlockInterface
* @throws LocalizedException
* @return array|mixed
*/
protected function getPriceRender()
public function getDisplayAdditional()
{
return $this->getLayout()->getBlock('product.price.render.default');
if ($this->getSlider()) {
$display = $this->getSlider()->getDisplayAdditional();
} else {
$display = $this->_helperData->getModuleConfig('general/display_information');
}

if (!is_array($display)) {
$display = explode(',', $display);
}

return $display;
}

/**
Expand All @@ -242,18 +254,18 @@ public function getProductPriceHtml(
if (!isset($arguments['zone'])) {
$arguments['zone'] = $renderZone;
}
$arguments['price_id'] = isset($arguments['price_id'])
$arguments['price_id'] = isset($arguments['price_id'])
? $arguments['price_id']
: 'old-price-' . $product->getId() . '-' . $priceType;
$arguments['include_container'] = isset($arguments['include_container'])
$arguments['include_container'] = isset($arguments['include_container'])
? $arguments['include_container']
: true;
$arguments['display_minimal_price'] = isset($arguments['display_minimal_price'])
? $arguments['display_minimal_price']
: true;

/** @var Render $priceRender */
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$priceRender = $this->getPriceRender();
if (!$priceRender) {
$priceRender = $this->getLayout()->createBlock(
Render::class,
Expand All @@ -269,34 +281,42 @@ public function getProductPriceHtml(
);
}

/**
* @return bool|\Magento\Framework\View\Element\BlockInterface
* @throws LocalizedException
*/
protected function getPriceRender()
{
return $this->getLayout()->getBlock('product.price.render.default');
}

/**
* @return bool
* @return mixed
*/
public function canShowReview()
private function getPriceCurrency()
{
return in_array(Additional::SHOW_REVIEW, $this->getDisplayAdditional(), true);
if ($this->priceCurrency === null) {
$this->priceCurrency = ObjectManager::getInstance()
->get(PriceCurrencyInterface::class);
}

return $this->priceCurrency;
}

/**
* @return bool
*/
public function canShowAddToCart()
public function canShowReview()
{
return in_array(Additional::SHOW_CART, $this->getDisplayAdditional(), true);
return in_array(Additional::SHOW_REVIEW, $this->getDisplayAdditional(), true);
}

/**
* Get Slider Id
* @return string
* @return bool
*/
public function getSliderId()
public function canShowAddToCart()
{
if ($this->getSlider()) {
return $this->getSlider()->getSliderId();
}

return uniqid('-', false);
return in_array(Additional::SHOW_CART, $this->getDisplayAdditional(), true);
}

/**
Expand Down Expand Up @@ -341,7 +361,7 @@ public function getDescription()
public function getAllOptions()
{
$sliderOptions = '';
$allConfig = $this->_helperData->getModuleConfig('slider_design');
$allConfig = $this->_helperData->getModuleConfig('slider_design');

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

/**
* @return mixed
*/
abstract public function getProductCollection();

/**
* Get Product Count is displayed
*
Expand All @@ -462,4 +487,53 @@ public function getProductsCount()

return 5;
}

/**
* @param $collection
*
* @return array
*/
public function getProductParentIds($collection)
{
$productIds = [];

foreach ($collection as $product) {
if (isset($product->getData()['entity_id'])) {
$productId = $product->getData()['entity_id'];
} else {
$productId = $product->getProductId();
}

$parentIdsGroup = $this->grouped->getParentIdsByChild($productId);
$parentIdsConfig = $this->configurable->getParentIdsByChild($productId);

if (!empty($parentIdsGroup)) {
$productIds[] = $parentIdsGroup;
} elseif (!empty($parentIdsConfig)) {
$productIds[] = $parentIdsConfig[0];
} else {
$productIds[] = $productId;
}
}

return $productIds;
}

/**
* @return bool|\Magento\Framework\View\Element\BlockInterface|\Magento\Framework\View\Element\RendererList
* @throws LocalizedException
*/
protected function getDetailsRendererList()
{
if (empty($this->rendererListBlock)) {
$layout = $this->layoutFactory->create(['cacheable' => false]);
$layout->getUpdate()->addHandle('catalog_widget_product_list')->load();
$layout->generateXml();
$layout->generateElements();

$this->rendererListBlock = $layout->getBlock('category.product.type.widget.details.renderers');
}

return $this->rendererListBlock;
}
}
Loading

0 comments on commit 8501db1

Please sign in to comment.