Skip to content

Commit

Permalink
Merge pull request #88 from mageplaza/2.3-develop
Browse files Browse the repository at this point in the history
v2.0.10
  • Loading branch information
DHT-Uri authored Jul 24, 2021
2 parents c0baca9 + f5169e9 commit ed59f77
Show file tree
Hide file tree
Showing 15 changed files with 547 additions and 293 deletions.
198 changes: 136 additions & 62 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,6 +119,9 @@ public function __construct(
Data $helperData,
HttpContext $httpContext,
EncoderInterface $urlEncoder,
Grouped $grouped,
Configurable $configurable,
LayoutFactory $layoutFactory,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
Expand All @@ -108,30 +130,13 @@ public function __construct(
$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,34 +155,31 @@ public function getCacheKeyInfo()
}

/**
* @return array|mixed
* {@inheritdoc}
*/
public function getDisplayAdditional()
protected function _construct()
{
if ($this->getSlider()) {
$display = $this->getSlider()->getDisplayAdditional();
} else {
$display = $this->_helperData->getModuleConfig('general/display_information');
}
parent::_construct();

if (!is_array($display)) {
$display = explode(',', $display);
}
$this->addData([
'cache_lifetime' => $this->getSlider() ? $this->getSlider()->getTimeCache() : 86400,
'cache_tags' => [Product::CACHE_TAG]
]);

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

/**
* @return mixed
* Get Slider Id
* @return string
*/
private function getPriceCurrency()
public function getSliderId()
{
if ($this->priceCurrency === null) {
$this->priceCurrency = ObjectManager::getInstance()
->get(PriceCurrencyInterface::class);
if ($this->getSlider()) {
return $this->getSlider()->getSliderId();
}

return $this->priceCurrency;
return uniqid('-', false);
}

/**
Expand Down Expand Up @@ -209,12 +211,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 Down Expand Up @@ -246,7 +257,7 @@ public function getProductPriceHtml(
: 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 @@ -263,32 +274,41 @@ public function getProductPriceHtml(
}

/**
* @return bool
* @return bool|\Magento\Framework\View\Element\BlockInterface
* @throws LocalizedException
*/
public function canShowReview()
protected function getPriceRender()
{
return in_array(Additional::SHOW_REVIEW, $this->getDisplayAdditional(), true);
return $this->getLayout()->getBlock('product.price.render.default');
}

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

return $this->priceCurrency;
}

/**
* Get Slider Id
* @return string
* @return bool
*/
public function getSliderId()
public function canShowReview()
{
if ($this->getSlider()) {
return $this->getSlider()->getSliderId();
}
return in_array(Additional::SHOW_REVIEW, $this->getDisplayAdditional(), true);
}

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

/**
Expand Down Expand Up @@ -437,6 +457,11 @@ public function getIdentities()
return $identities ?: [Product::CACHE_TAG];
}

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

/**
* Get Product Count is displayed
*
Expand All @@ -454,4 +479,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;
}
}
4 changes: 2 additions & 2 deletions Block/Adminhtml/Config/Field/Responsive.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Responsive extends AbstractFieldArray
*/
protected function _prepareToRender()
{
$this->addColumn('size', ['label' => __('Screen size max'), 'renderer' => false]);
$this->addColumn('items', ['label' => __('Number of items'), 'renderer' => false]);
$this->addColumn('size', ['label' => __('Screen size max'), 'renderer' => false, 'class' => 'required-entry validate-number validate-greater-than-zero']);
$this->addColumn('items', ['label' => __('Number of items'), 'renderer' => false, 'class' => 'required-entry validate-number validate-greater-than-zero']);

$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
Expand Down
1 change: 1 addition & 0 deletions Block/Adminhtml/Slider/Edit/Tab/Design.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ protected function _prepareForm()
'name' => 'limit_number',
'label' => __('Limit the number of products'),
'title' => __('Limit the number of products'),
'class' => 'validate-number validate-zero-or-greater'
]);

$fieldset->addField('display_additional', 'multiselect', [
Expand Down
Loading

0 comments on commit ed59f77

Please sign in to comment.