From 01e44bf77c7b81ad8aba62141a621cac811e1e9e Mon Sep 17 00:00:00 2001 From: Vu Van Tu Date: Thu, 10 Dec 2020 13:56:21 +0700 Subject: [PATCH 01/19] fix Most Viewed block showing disabled products --- Block/MostViewedProducts.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Block/MostViewedProducts.php b/Block/MostViewedProducts.php index 2c30c57..7acc627 100644 --- a/Block/MostViewedProducts.php +++ b/Block/MostViewedProducts.php @@ -87,6 +87,7 @@ public function getProductCollection() $collection = $this->_mostViewedProductsFactory->create() ->setStoreId($this->getStoreId())->addViewsCount() ->addStoreFilter($this->getStoreId()) + ->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds()) ->setPageSize($this->getProductsCount()); $this->_addProductAttributesAndPrices($collection); From 4bf68a050bbde1a73a6d65a94b7a4d3ffff46e87 Mon Sep 17 00:00:00 2001 From: Vu Van Tu Date: Fri, 11 Dec 2020 10:31:05 +0700 Subject: [PATCH 02/19] fix feature attribute --- Setup/InstallData.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Setup/InstallData.php b/Setup/InstallData.php index b49dd1b..e00cbbd 100644 --- a/Setup/InstallData.php +++ b/Setup/InstallData.php @@ -23,6 +23,7 @@ use Magento\Catalog\Model\Product; use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; +use Magento\Eav\Model\Entity\Attribute\Source\Boolean; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; @@ -61,14 +62,13 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute(Product::ENTITY, 'is_featured', [ + 'group' => 'General', 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'Featured Product', - 'note' => '', 'input' => 'boolean', - 'class' => '', - 'source' => '', + 'source' => Boolean::class, 'global' => ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => false, From cbe3b652943769437a44053ff5e81cd1729697c6 Mon Sep 17 00:00:00 2001 From: Duong Huy Toan Date: Tue, 5 Jan 2021 17:49:33 +0700 Subject: [PATCH 03/19] optimized the OnSale Products --- Block/OnSaleProduct.php | 77 ++++++++++++----------------------------- 1 file changed, 22 insertions(+), 55 deletions(-) diff --git a/Block/OnSaleProduct.php b/Block/OnSaleProduct.php index ff5c48a..c6b8c83 100644 --- a/Block/OnSaleProduct.php +++ b/Block/OnSaleProduct.php @@ -79,64 +79,31 @@ public function __construct( */ public function getProductCollection() { - $date = strtotime($this->_dateTimeStore->gmtDate()); - $collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*'); - $productIds = []; - - foreach ($collection as $product) { - if ($product->getTypeId() === 'configurable' && $product->getVisibility() != 1) { - $_children = $product->getTypeInstance()->getUsedProducts($product); - foreach ($_children as $child) { - $specialPrice = (float)$child->getSpecialPrice(); - if ($specialPrice) { - if ($specialPrice < ((float)$child->getPrice())) { - $fromDate = strtotime($child->getSpecialFromDate()); - if (!is_null($child->getSpecialToDate())) { - $toDate = strtotime($child->getSpecialToDate()); - if ($toDate > $date) { - $productIds[] = $product->getId(); - } - } else { - if ($fromDate < $date) { - $productIds[] = $product->getId(); - } - } - } - } - } - - } elseif ($product->getTypeId() === 'simple' && $product->getVisibility() != 1) { - $specialPriceSp = (float)$product->getData('special_price'); - if ($specialPriceSp) { - if ($specialPriceSp < ((float)$product->getPrice())) { - $fromDateSp = strtotime($product->getSpecialFromDate()); - if (!is_null($product->getSpecialToDate())) { - $toDateSp = strtotime($product->getSpecialToDate()); - if ($toDateSp > $date) { - $productIds[] = $product->getId(); - } - } else { - if ($fromDateSp < $date) { - $productIds[] = $product->getId(); - } - } - } - } - } - } - - if (empty($productIds)) { - return null; - } - - $collectionClone = $this->_productCollectionFactory->create()->addIdFilter($productIds); - $collectionClone->addMinimalPrice() + $productCollection = $this->_productCollectionFactory->create(); + $productCollection + ->addMinimalPrice() ->addFinalPrice() ->addTaxPercents() - ->addAttributeToSelect('*') - ->addStoreFilter($this->getStoreId())->setPageSize($this->getProductsCount()); + ->addStoreFilter($this->getStoreId()) + ->addAttributeToFilter('visibility', ['neq' => 1]) + ->addAttributeToFilter('status', 1) + ->addAttributeToSelect('name') + ->addAttributeToSelect('image') + ->addAttributeToSelect('small_image') + ->addAttributeToSelect('thumbnail') + ->addAttributeToSelect('special_from_date') + ->addAttributeToSelect('special_to_date') + ->addAttributeToFilter('special_price', ['gt' => 0]) + ->addAttributeToSort( + 'minimal_price', + 'asc' + ) + ->setPageSize($this->getProductsCount()); + $productCollection->getSelect()->where( + 'price_index.final_price < price_index.price' + ); - return $collectionClone; + return $productCollection; } } From 6bfee5773e2113ff755e1acc837da7e5c1ce430d Mon Sep 17 00:00:00 2001 From: Duong Huy Toan Date: Wed, 3 Mar 2021 16:25:03 +0700 Subject: [PATCH 04/19] =?UTF-8?q?Hi=E1=BB=83n=20th=E1=BB=8B=20c=C3=A1c=20S?= =?UTF-8?q?P=20config=20(OnSale)=20v=C3=A0=20SP=20cha=20khi=20ch=E1=BB=8Dn?= =?UTF-8?q?=20SP=20con?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Block/AbstractSlider.php | 179 ++++++++++++++++++++++------------- Block/BestSellerProducts.php | 18 ++-- Block/CategoryId.php | 12 ++- Block/CustomProducts.php | 65 ++++++++++++- Block/MostViewedProducts.php | 24 ++++- Block/OnSaleProduct.php | 26 +++-- Block/RecentProducts.php | 12 ++- Block/Widget/Slider.php | 9 +- Block/WishlistProducts.php | 20 ++-- 9 files changed, 265 insertions(+), 100 deletions(-) diff --git a/Block/AbstractSlider.php b/Block/AbstractSlider.php index c542073..722d9ce 100644 --- a/Block/AbstractSlider.php +++ b/Block/AbstractSlider.php @@ -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; @@ -38,6 +39,7 @@ use Magento\Framework\Pricing\Render; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Magento\Widget\Block\BlockInterface; use Mageplaza\Productslider\Helper\Data; use Mageplaza\Productslider\Model\Config\Source\Additional; @@ -48,41 +50,45 @@ */ 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 + */ + private $priceCurrency; /** * AbstractSlider constructor. - * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -90,6 +96,8 @@ abstract class AbstractSlider extends AbstractProduct implements BlockInterface, * @param Data $helperData * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder + * @param Grouped $grouped + * @param Configurable $configurable * @param array $data */ public function __construct( @@ -100,38 +108,23 @@ public function __construct( Data $helperData, HttpContext $httpContext, EncoderInterface $urlEncoder, + Grouped $grouped, + Configurable $configurable, array $data = [] - ) { + ) + { $this->_productCollectionFactory = $productCollectionFactory; $this->_catalogProductVisibility = $catalogProductVisibility; $this->_date = $dateTime; $this->_helperData = $helperData; $this->httpContext = $httpContext; $this->urlEncoder = $urlEncoder; + $this->grouped = $grouped; + $this->configurable = $configurable; 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 * @@ -150,41 +143,36 @@ public function getCacheKeyInfo() } /** - * @return Data + * @return mixed */ - public function getHelperData() + private function getPriceCurrency() { - return $this->_helperData; + if ($this->priceCurrency === null) { + $this->priceCurrency = ObjectManager::getInstance() + ->get(PriceCurrencyInterface::class); + } + return $this->priceCurrency; } /** - * @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; } /** @@ -216,12 +204,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; } /** @@ -238,7 +235,8 @@ public function getProductPriceHtml( $priceType = null, $renderZone = Render::ZONE_ITEM_LIST, array $arguments = [] - ) { + ) + { if (!isset($arguments['zone'])) { $arguments['zone'] = $renderZone; } @@ -269,7 +267,6 @@ public function getProductPriceHtml( ); } - /** * @return bool */ @@ -286,19 +283,6 @@ public function canShowAddToCart() return in_array(Additional::SHOW_CART, $this->getDisplayAdditional(), true); } - /** - * Get Slider Id - * @return string - */ - public function getSliderId() - { - if ($this->getSlider()) { - return $this->getSlider()->getSliderId(); - } - - return uniqid('-', false); - } - /** * Get Slider Title * @@ -445,6 +429,11 @@ public function getIdentities() return $identities ?: [Product::CACHE_TAG]; } + /** + * @return mixed + */ + abstract public function getProductCollection(); + /** * Get Product Count is displayed * @@ -462,4 +451,58 @@ public function getProductsCount() return 5; } + + /** + * @param $collection + * @return array + */ + public function getProductParentIds($collection) + { + + $productIds = array(); + 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; + } + + /** + * {@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 bool|\Magento\Framework\View\Element\BlockInterface + * @throws LocalizedException + */ + protected function getPriceRender() + { + return $this->getLayout()->getBlock('product.price.render.default'); + } } diff --git a/Block/BestSellerProducts.php b/Block/BestSellerProducts.php index 9807862..8334f12 100644 --- a/Block/BestSellerProducts.php +++ b/Block/BestSellerProducts.php @@ -24,9 +24,11 @@ use Magento\Catalog\Block\Product\Context; use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory as BestSellersCollectionFactory; use Mageplaza\Productslider\Helper\Data; @@ -43,7 +45,6 @@ class BestSellerProducts extends AbstractSlider /** * BestSellerProducts constructor. - * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -52,6 +53,8 @@ class BestSellerProducts extends AbstractSlider * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder * @param BestSellersCollectionFactory $bestSellersCollectionFactory + * @param Grouped $grouped + * @param Configurable $configurable * @param array $data */ public function __construct( @@ -63,8 +66,11 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, BestSellersCollectionFactory $bestSellersCollectionFactory, + Grouped $grouped, + Configurable $configurable, array $data = [] - ) { + ) + { $this->_bestSellersCollectionFactory = $bestSellersCollectionFactory; parent::__construct( @@ -75,6 +81,8 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, $data ); } @@ -85,16 +93,12 @@ public function __construct( */ public function getProductCollection() { - $productIds = []; $bestSellers = $this->_bestSellersCollectionFactory->create() ->setModel('Magento\Catalog\Model\Product') ->addStoreFilter($this->getStoreId()) ->setPeriod('month'); - foreach ($bestSellers as $product) { - $productIds[] = $product->getProductId(); - } - + $productIds = $this->getProductParentIds($bestSellers); if (empty($productIds)) { return null; } diff --git a/Block/CategoryId.php b/Block/CategoryId.php index 4a957a9..bdc877c 100644 --- a/Block/CategoryId.php +++ b/Block/CategoryId.php @@ -25,9 +25,11 @@ use Magento\Catalog\Model\CategoryFactory; use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Helper\Data; /** @@ -43,7 +45,6 @@ class CategoryId extends AbstractSlider /** * CategoryId constructor. - * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -52,6 +53,8 @@ class CategoryId extends AbstractSlider * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder * @param CategoryFactory $categoryFactory + * @param Grouped $grouped + * @param Configurable $configurable * @param array $data */ public function __construct( @@ -63,8 +66,11 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, CategoryFactory $categoryFactory, + Grouped $grouped, + Configurable $configurable, array $data = [] - ) { + ) + { $this->_categoryFactory = $categoryFactory; parent::__construct( @@ -75,6 +81,8 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, $data ); } diff --git a/Block/CustomProducts.php b/Block/CustomProducts.php index aacb6f6..3006d75 100644 --- a/Block/CustomProducts.php +++ b/Block/CustomProducts.php @@ -21,14 +21,67 @@ namespace Mageplaza\Productslider\Block; +use Magento\Catalog\Block\Product\Context; +use Magento\Catalog\Model\Product\Visibility; +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; +use Magento\Framework\App\Http\Context as HttpContext; +use Magento\Framework\Stdlib\DateTime\DateTime; +use Magento\Framework\Url\EncoderInterface; +use Magento\GroupedProduct\Model\Product\Type\Grouped; +use Mageplaza\Productslider\Helper\Data; + + /** * Class CustomProducts * @package Mageplaza\Productslider\Block */ class CustomProducts extends AbstractSlider { + /** - * @return $this|mixed + * CustomProducts constructor. + * @param Context $context + * @param CollectionFactory $productCollectionFactory + * @param Visibility $catalogProductVisibility + * @param DateTime $dateTime + * @param Data $helperData + * @param HttpContext $httpContext + * @param EncoderInterface $urlEncoder + * @param Grouped $grouped + * @param Configurable $configurable + * @param array $data + */ + public function __construct( + Context $context, + CollectionFactory $productCollectionFactory, + Visibility $catalogProductVisibility, + DateTime $dateTime, + Data $helperData, + HttpContext $httpContext, + EncoderInterface $urlEncoder, + Grouped $grouped, + Configurable $configurable, + array $data = [] + ) + { + + parent::__construct( + $context, + $productCollectionFactory, + $catalogProductVisibility, + $dateTime, + $helperData, + $httpContext, + $urlEncoder, + $grouped, + $configurable, + $data + ); + } + + /** + * @return \Magento\Catalog\Model\ResourceModel\Product\Collection|mixed|null */ public function getProductCollection() { @@ -44,7 +97,15 @@ public function getProductCollection() $collection = $this->_productCollectionFactory->create() ->addIdFilter($productIds) ->setPageSize($this->getProductsCount()); - $this->_addProductAttributesAndPrices($collection); + + $mpProductIds = $this->getProductParentIds($collection); + + $collection = $this->_productCollectionFactory->create()->addIdFilter($mpProductIds); + $collection->addMinimalPrice() + ->addFinalPrice() + ->addTaxPercents() + ->addAttributeToSelect('*') + ->addStoreFilter($this->getStoreId())->setPageSize($this->getProductsCount()); return $collection; } diff --git a/Block/MostViewedProducts.php b/Block/MostViewedProducts.php index 7acc627..dbcd5a9 100644 --- a/Block/MostViewedProducts.php +++ b/Block/MostViewedProducts.php @@ -24,9 +24,11 @@ use Magento\Catalog\Block\Product\Context; use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Helper\Data; use Mageplaza\Productslider\Model\ResourceModel\Report\Product\CollectionFactory as MostViewedCollectionFactory; @@ -43,14 +45,16 @@ class MostViewedProducts extends AbstractSlider /** * MostViewedProducts constructor. - * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility * @param DateTime $dateTime * @param Data $helperData * @param HttpContext $httpContext + * @param EncoderInterface $urlEncoder * @param MostViewedCollectionFactory $mostViewedProductsFactory + * @param Grouped $grouped + * @param Configurable $configurable * @param array $data */ public function __construct( @@ -62,8 +66,11 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, MostViewedCollectionFactory $mostViewedProductsFactory, + Grouped $grouped, + Configurable $configurable, array $data = [] - ) { + ) + { $this->_mostViewedProductsFactory = $mostViewedProductsFactory; parent::__construct( @@ -74,6 +81,8 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, $data ); } @@ -87,9 +96,16 @@ public function getProductCollection() $collection = $this->_mostViewedProductsFactory->create() ->setStoreId($this->getStoreId())->addViewsCount() ->addStoreFilter($this->getStoreId()) - ->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds()) ->setPageSize($this->getProductsCount()); - $this->_addProductAttributesAndPrices($collection); + + $productIds = $this->getProductParentIds($collection); + + $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds); + $collection->addMinimalPrice() + ->addFinalPrice() + ->addTaxPercents() + ->addAttributeToSelect('*') + ->addStoreFilter($this->getStoreId()); return $collection; } diff --git a/Block/OnSaleProduct.php b/Block/OnSaleProduct.php index c6b8c83..4c5087a 100644 --- a/Block/OnSaleProduct.php +++ b/Block/OnSaleProduct.php @@ -24,9 +24,11 @@ use Magento\Catalog\Block\Product\Context; use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Helper\Data; /** @@ -49,6 +51,8 @@ class OnSaleProduct extends AbstractSlider * @param Data $helperData * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder + * @param Grouped $grouped + * @param Configurable $configurable * @param array $data */ public function __construct( @@ -59,8 +63,11 @@ public function __construct( Data $helperData, HttpContext $httpContext, EncoderInterface $urlEncoder, + Grouped $grouped, + Configurable $configurable, array $data = [] - ) { + ) + { $this->_dateTimeStore = $dateTime; parent::__construct( $context, @@ -70,6 +77,8 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, $data ); } @@ -85,12 +94,6 @@ public function getProductCollection() ->addFinalPrice() ->addTaxPercents() ->addStoreFilter($this->getStoreId()) - ->addAttributeToFilter('visibility', ['neq' => 1]) - ->addAttributeToFilter('status', 1) - ->addAttributeToSelect('name') - ->addAttributeToSelect('image') - ->addAttributeToSelect('small_image') - ->addAttributeToSelect('thumbnail') ->addAttributeToSelect('special_from_date') ->addAttributeToSelect('special_to_date') ->addAttributeToFilter('special_price', ['gt' => 0]) @@ -104,6 +107,15 @@ public function getProductCollection() 'price_index.final_price < price_index.price' ); + $productIds = $this->getProductParentIds($productCollection); + $productCollection = $this->_productCollectionFactory->create()->addIdFilter($productIds); + + $productCollection->addAttributeToFilter('visibility', ['neq' => 1]) + ->addAttributeToFilter('status', 1) + ->addAttributeToSelect('*') + ->addStoreFilter($this->getStoreId()) + ->setPageSize($this->getProductsCount()); + return $productCollection; } } diff --git a/Block/RecentProducts.php b/Block/RecentProducts.php index 3fff255..6a6f7b1 100644 --- a/Block/RecentProducts.php +++ b/Block/RecentProducts.php @@ -24,9 +24,11 @@ use Magento\Catalog\Block\Product\Context; use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Magento\Reports\Block\Product\Viewed as ReportProductViewed; use Mageplaza\Productslider\Helper\Data; @@ -43,7 +45,6 @@ class RecentProducts extends AbstractSlider /** * RecentProducts constructor. - * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -52,6 +53,8 @@ class RecentProducts extends AbstractSlider * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder * @param ReportProductViewed $reportProductViewed + * @param Grouped $grouped + * @param Configurable $configurable * @param array $data */ public function __construct( @@ -63,8 +66,11 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, ReportProductViewed $reportProductViewed, + Grouped $grouped, + Configurable $configurable, array $data = [] - ) { + ) + { $this->reportProductViewed = $reportProductViewed; parent::__construct( @@ -75,6 +81,8 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, $data ); } diff --git a/Block/Widget/Slider.php b/Block/Widget/Slider.php index 7b34873..066f43d 100644 --- a/Block/Widget/Slider.php +++ b/Block/Widget/Slider.php @@ -31,6 +31,8 @@ use Mageplaza\Productslider\Block\AbstractSlider; use Mageplaza\Productslider\Helper\Data; use Mageplaza\Productslider\Model\Config\Source\ProductType; +use Magento\GroupedProduct\Model\Product\Type\Grouped; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; /** * Class Slider @@ -52,7 +54,6 @@ class Slider extends AbstractSlider /** * Slider constructor. - * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -61,6 +62,8 @@ class Slider extends AbstractSlider * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder * @param ProductType $productType + * @param Grouped $grouped + * @param Configurable $configurable * @param array $data */ public function __construct( @@ -72,6 +75,8 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, ProductType $productType, + Grouped $grouped, + Configurable $configurable, array $data = [] ) { parent::__construct( @@ -82,6 +87,8 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, $data ); $this->productType = $productType; diff --git a/Block/WishlistProducts.php b/Block/WishlistProducts.php index 67e8775..c14d763 100644 --- a/Block/WishlistProducts.php +++ b/Block/WishlistProducts.php @@ -24,10 +24,12 @@ use Magento\Catalog\Block\Product\Context; use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Customer\Model\Session as CustomerSession; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistCollectionFactory; use Mageplaza\Productslider\Helper\Data; @@ -49,7 +51,6 @@ class WishlistProducts extends AbstractSlider /** * WishlistProducts constructor. - * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -59,6 +60,8 @@ class WishlistProducts extends AbstractSlider * @param EncoderInterface $urlEncoder * @param WishlistCollectionFactory $wishlistCollectionFactory * @param CustomerSession $_customerSession + * @param Grouped $grouped + * @param Configurable $configurable * @param array $data */ public function __construct( @@ -71,8 +74,11 @@ public function __construct( EncoderInterface $urlEncoder, WishlistCollectionFactory $wishlistCollectionFactory, CustomerSession $_customerSession, + Grouped $grouped, + Configurable $configurable, array $data = [] - ) { + ) + { $this->_wishlistCollectionFactory = $wishlistCollectionFactory; $this->_customerSession = $_customerSession; @@ -84,6 +90,8 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, $data ); } @@ -98,12 +106,10 @@ public function getProductCollection() if ($this->_customerSession->isLoggedIn()) { $wishlist = $this->_wishlistCollectionFactory->create() ->addCustomerIdFilter($this->_customerSession->getCustomerId()); - $productIds = null; - foreach ($wishlist as $product) { - $productIds[] = $product->getProductId(); - } - $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds); + $mpProductIds = $this->getProductParentIds($wishlist); + + $collection = $this->_productCollectionFactory->create()->addIdFilter($mpProductIds); $collection = $this->_addProductAttributesAndPrices($collection) ->addStoreFilter($this->getStoreId()) ->setPageSize($this->getProductsCount()); From b01d4ad6de20ab0458b311466b83d2bb7d3c95b2 Mon Sep 17 00:00:00 2001 From: Duong Huy Toan Date: Fri, 5 Mar 2021 14:26:40 +0700 Subject: [PATCH 05/19] =?UTF-8?q?Hi=E1=BB=83n=20th=E1=BB=8B=20swatches=20c?= =?UTF-8?q?=C3=A1c=20s=E1=BA=A3n=20ph=E1=BA=A9m=20config.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Block/AbstractSlider.php | 30 ++++++++++++++++ Block/BestSellerProducts.php | 4 +++ Block/CategoryId.php | 4 +++ Block/CustomProducts.php | 4 +++ Block/MostViewedProducts.php | 4 +++ Block/OnSaleProduct.php | 4 +++ Block/RecentProducts.php | 4 +++ Block/Widget/Slider.php | 69 +++++++++++++++++++----------------- Block/WishlistProducts.php | 4 +++ 9 files changed, 95 insertions(+), 32 deletions(-) diff --git a/Block/AbstractSlider.php b/Block/AbstractSlider.php index 722d9ce..9c6170e 100644 --- a/Block/AbstractSlider.php +++ b/Block/AbstractSlider.php @@ -39,6 +39,7 @@ 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; @@ -82,10 +83,18 @@ abstract class AbstractSlider extends AbstractProduct implements BlockInterface, * @var Configurable */ protected $configurable; + /** + * @var + */ + protected $rendererListBlock; /** * @var */ private $priceCurrency; + /** + * @var LayoutFactory + */ + private $layoutFactory; /** * AbstractSlider constructor. @@ -98,6 +107,7 @@ abstract class AbstractSlider extends AbstractProduct implements BlockInterface, * @param EncoderInterface $urlEncoder * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -110,6 +120,7 @@ public function __construct( EncoderInterface $urlEncoder, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { @@ -121,6 +132,7 @@ public function __construct( $this->urlEncoder = $urlEncoder; $this->grouped = $grouped; $this->configurable = $configurable; + $this->layoutFactory = $layoutFactory; parent::__construct($context, $data); } @@ -482,6 +494,24 @@ public function getProductParentIds($collection) 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; + } + /** * {@inheritdoc} */ diff --git a/Block/BestSellerProducts.php b/Block/BestSellerProducts.php index 8334f12..0f3f196 100644 --- a/Block/BestSellerProducts.php +++ b/Block/BestSellerProducts.php @@ -28,6 +28,7 @@ use Magento\Framework\App\Http\Context as HttpContext; 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\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory as BestSellersCollectionFactory; use Mageplaza\Productslider\Helper\Data; @@ -55,6 +56,7 @@ class BestSellerProducts extends AbstractSlider * @param BestSellersCollectionFactory $bestSellersCollectionFactory * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -68,6 +70,7 @@ public function __construct( BestSellersCollectionFactory $bestSellersCollectionFactory, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { @@ -83,6 +86,7 @@ public function __construct( $urlEncoder, $grouped, $configurable, + $layoutFactory, $data ); } diff --git a/Block/CategoryId.php b/Block/CategoryId.php index bdc877c..a005345 100644 --- a/Block/CategoryId.php +++ b/Block/CategoryId.php @@ -29,6 +29,7 @@ use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\Framework\View\LayoutFactory; use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Helper\Data; @@ -55,6 +56,7 @@ class CategoryId extends AbstractSlider * @param CategoryFactory $categoryFactory * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -68,6 +70,7 @@ public function __construct( CategoryFactory $categoryFactory, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { @@ -83,6 +86,7 @@ public function __construct( $urlEncoder, $grouped, $configurable, + $layoutFactory, $data ); } diff --git a/Block/CustomProducts.php b/Block/CustomProducts.php index 3006d75..fde40af 100644 --- a/Block/CustomProducts.php +++ b/Block/CustomProducts.php @@ -28,6 +28,7 @@ use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\Framework\View\LayoutFactory; use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Helper\Data; @@ -50,6 +51,7 @@ class CustomProducts extends AbstractSlider * @param EncoderInterface $urlEncoder * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -62,6 +64,7 @@ public function __construct( EncoderInterface $urlEncoder, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { @@ -76,6 +79,7 @@ public function __construct( $urlEncoder, $grouped, $configurable, + $layoutFactory, $data ); } diff --git a/Block/MostViewedProducts.php b/Block/MostViewedProducts.php index dbcd5a9..41c042f 100644 --- a/Block/MostViewedProducts.php +++ b/Block/MostViewedProducts.php @@ -28,6 +28,7 @@ use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\Framework\View\LayoutFactory; use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Helper\Data; use Mageplaza\Productslider\Model\ResourceModel\Report\Product\CollectionFactory as MostViewedCollectionFactory; @@ -55,6 +56,7 @@ class MostViewedProducts extends AbstractSlider * @param MostViewedCollectionFactory $mostViewedProductsFactory * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -68,6 +70,7 @@ public function __construct( MostViewedCollectionFactory $mostViewedProductsFactory, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { @@ -83,6 +86,7 @@ public function __construct( $urlEncoder, $grouped, $configurable, + $layoutFactory, $data ); } diff --git a/Block/OnSaleProduct.php b/Block/OnSaleProduct.php index 4c5087a..98bb872 100644 --- a/Block/OnSaleProduct.php +++ b/Block/OnSaleProduct.php @@ -28,6 +28,7 @@ use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\Framework\View\LayoutFactory; use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Helper\Data; @@ -53,6 +54,7 @@ class OnSaleProduct extends AbstractSlider * @param EncoderInterface $urlEncoder * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -65,6 +67,7 @@ public function __construct( EncoderInterface $urlEncoder, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { @@ -79,6 +82,7 @@ public function __construct( $urlEncoder, $grouped, $configurable, + $layoutFactory, $data ); } diff --git a/Block/RecentProducts.php b/Block/RecentProducts.php index 6a6f7b1..ae21200 100644 --- a/Block/RecentProducts.php +++ b/Block/RecentProducts.php @@ -28,6 +28,7 @@ use Magento\Framework\App\Http\Context as HttpContext; 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\Reports\Block\Product\Viewed as ReportProductViewed; use Mageplaza\Productslider\Helper\Data; @@ -55,6 +56,7 @@ class RecentProducts extends AbstractSlider * @param ReportProductViewed $reportProductViewed * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -68,6 +70,7 @@ public function __construct( ReportProductViewed $reportProductViewed, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { @@ -83,6 +86,7 @@ public function __construct( $urlEncoder, $grouped, $configurable, + $layoutFactory, $data ); } diff --git a/Block/Widget/Slider.php b/Block/Widget/Slider.php index 066f43d..8cf340a 100644 --- a/Block/Widget/Slider.php +++ b/Block/Widget/Slider.php @@ -24,15 +24,16 @@ use Magento\Catalog\Block\Product\Context; use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Url\EncoderInterface; +use Magento\Framework\View\LayoutFactory; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Block\AbstractSlider; use Mageplaza\Productslider\Helper\Data; use Mageplaza\Productslider\Model\Config\Source\ProductType; -use Magento\GroupedProduct\Model\Product\Type\Grouped; -use Magento\ConfigurableProduct\Model\Product\Type\Configurable; /** * Class Slider @@ -64,6 +65,7 @@ class Slider extends AbstractSlider * @param ProductType $productType * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -77,8 +79,10 @@ public function __construct( ProductType $productType, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] - ) { + ) + { parent::__construct( $context, $productCollectionFactory, @@ -89,6 +93,7 @@ public function __construct( $urlEncoder, $grouped, $configurable, + $layoutFactory, $data ); $this->productType = $productType; @@ -125,6 +130,35 @@ public function getProductCollection() return $collection; } + /** + * Retrieve how many products should be displayed on page + * + * @return int + */ + protected function getPageSize() + { + return $this->getProductsCount(); + } + + /** + * Get limited number + * @return int|mixed + */ + public function getProductsCount() + { + return $this->getData('products_count') ?: 10; + } + + /** + * Get number of current page based on query value + * + * @return int + */ + public function getCurrentPage() + { + return abs((int)$this->getRequest()->getParam($this->getData('page_var_name'))); + } + /** * Get key pieces for caching block content * @@ -171,35 +205,6 @@ public function getDisplayType() return $this->getData('product_type'); } - /** - * Get number of current page based on query value - * - * @return int - */ - public function getCurrentPage() - { - return abs((int)$this->getRequest()->getParam($this->getData('page_var_name'))); - } - - /** - * Retrieve how many products should be displayed on page - * - * @return int - */ - protected function getPageSize() - { - return $this->getProductsCount(); - } - - /** - * Get limited number - * @return int|mixed - */ - public function getProductsCount() - { - return $this->getData('products_count') ?: 10; - } - /** * @return mixed */ diff --git a/Block/WishlistProducts.php b/Block/WishlistProducts.php index c14d763..1688295 100644 --- a/Block/WishlistProducts.php +++ b/Block/WishlistProducts.php @@ -29,6 +29,7 @@ use Magento\Framework\App\Http\Context as HttpContext; 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\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistCollectionFactory; use Mageplaza\Productslider\Helper\Data; @@ -62,6 +63,7 @@ class WishlistProducts extends AbstractSlider * @param CustomerSession $_customerSession * @param Grouped $grouped * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -76,6 +78,7 @@ public function __construct( CustomerSession $_customerSession, Grouped $grouped, Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { @@ -92,6 +95,7 @@ public function __construct( $urlEncoder, $grouped, $configurable, + $layoutFactory, $data ); } From e66ff5b8ba079e9440dbcc5c1a90a6f50c38ddac Mon Sep 17 00:00:00 2001 From: Duong Huy Toan Date: Fri, 5 Mar 2021 14:27:36 +0700 Subject: [PATCH 06/19] =?UTF-8?q?Hi=E1=BB=83n=20th=E1=BB=8B=20swatches=20c?= =?UTF-8?q?=C3=A1c=20s=E1=BA=A3n=20ph=E1=BA=A9m=20config.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- view/frontend/layout/default.xml | 57 ++++++++++++++++---------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml index 225c787..6bc6cdd 100644 --- a/view/frontend/layout/default.xml +++ b/view/frontend/layout/default.xml @@ -1,28 +1,29 @@ - - - - - - - - + + + + + + + + + From 87d1012d8760c7f10d8eae860a6c0b79a035c5f3 Mon Sep 17 00:00:00 2001 From: Vu Van Tu Date: Tue, 23 Mar 2021 10:31:52 +0700 Subject: [PATCH 07/19] clean code --- Block/AbstractSlider.php | 101 ++++++++++---------- Block/BestSellerProducts.php | 4 +- Block/CategoryId.php | 6 +- Block/CustomProducts.php | 4 +- Block/MostViewedProducts.php | 4 +- Block/NewProducts.php | 4 +- Block/OnSaleProduct.php | 8 +- Block/RecentProducts.php | 4 +- Block/WishlistProducts.php | 6 +- view/frontend/layout/default.xml | 29 ------ view/frontend/templates/productslider.phtml | 2 + 11 files changed, 73 insertions(+), 99 deletions(-) delete mode 100644 view/frontend/layout/default.xml diff --git a/Block/AbstractSlider.php b/Block/AbstractSlider.php index 9c6170e..b5ba9d7 100644 --- a/Block/AbstractSlider.php +++ b/Block/AbstractSlider.php @@ -98,6 +98,7 @@ abstract class AbstractSlider extends AbstractProduct implements BlockInterface, /** * AbstractSlider constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -122,17 +123,16 @@ public function __construct( 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->grouped = $grouped; - $this->configurable = $configurable; - $this->layoutFactory = $layoutFactory; + $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); } @@ -155,15 +155,18 @@ public function getCacheKeyInfo() } /** - * @return mixed + * {@inheritdoc} */ - private function getPriceCurrency() + protected function _construct() { - if ($this->priceCurrency === null) { - $this->priceCurrency = ObjectManager::getInstance() - ->get(PriceCurrencyInterface::class); - } - return $this->priceCurrency; + parent::_construct(); + + $this->addData([ + 'cache_lifetime' => $this->getSlider() ? $this->getSlider()->getTimeCache() : 86400, + 'cache_tags' => [Product::CACHE_TAG] + ]); + + $this->setTemplate('Mageplaza_Productslider::productslider.phtml'); } /** @@ -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), ] ]; @@ -247,15 +250,14 @@ public function getProductPriceHtml( $priceType = null, $renderZone = Render::ZONE_ITEM_LIST, array $arguments = [] - ) - { + ) { 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']) @@ -263,7 +265,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, @@ -279,6 +281,28 @@ public function getProductPriceHtml( ); } + /** + * @return bool|\Magento\Framework\View\Element\BlockInterface + * @throws LocalizedException + */ + protected function getPriceRender() + { + return $this->getLayout()->getBlock('product.price.render.default'); + } + + /** + * @return mixed + */ + private function getPriceCurrency() + { + if ($this->priceCurrency === null) { + $this->priceCurrency = ObjectManager::getInstance() + ->get(PriceCurrencyInterface::class); + } + + return $this->priceCurrency; + } + /** * @return bool */ @@ -337,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') { @@ -466,12 +490,13 @@ public function getProductsCount() /** * @param $collection + * * @return array */ public function getProductParentIds($collection) { + $productIds = []; - $productIds = array(); foreach ($collection as $product) { if (isset($product->getData()['entity_id'])) { $productId = $product->getData()['entity_id']; @@ -479,7 +504,7 @@ public function getProductParentIds($collection) $productId = $product->getProductId(); } - $parentIdsGroup = $this->grouped->getParentIdsByChild($productId); + $parentIdsGroup = $this->grouped->getParentIdsByChild($productId); $parentIdsConfig = $this->configurable->getParentIdsByChild($productId); if (!empty($parentIdsGroup)) { @@ -511,28 +536,4 @@ protected function getDetailsRendererList() return $this->rendererListBlock; } - - /** - * {@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 bool|\Magento\Framework\View\Element\BlockInterface - * @throws LocalizedException - */ - protected function getPriceRender() - { - return $this->getLayout()->getBlock('product.price.render.default'); - } } diff --git a/Block/BestSellerProducts.php b/Block/BestSellerProducts.php index 0f3f196..cbe3320 100644 --- a/Block/BestSellerProducts.php +++ b/Block/BestSellerProducts.php @@ -46,6 +46,7 @@ class BestSellerProducts extends AbstractSlider /** * BestSellerProducts constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -72,8 +73,7 @@ public function __construct( Configurable $configurable, LayoutFactory $layoutFactory, array $data = [] - ) - { + ) { $this->_bestSellersCollectionFactory = $bestSellersCollectionFactory; parent::__construct( diff --git a/Block/CategoryId.php b/Block/CategoryId.php index a005345..bb1e858 100644 --- a/Block/CategoryId.php +++ b/Block/CategoryId.php @@ -46,6 +46,7 @@ class CategoryId extends AbstractSlider /** * CategoryId constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -72,8 +73,7 @@ public function __construct( Configurable $configurable, LayoutFactory $layoutFactory, array $data = [] - ) - { + ) { $this->_categoryFactory = $categoryFactory; parent::__construct( @@ -118,7 +118,7 @@ public function getProductCollection() public function getProductIdsByCategory() { $productIds = []; - $catIds = $this->getSliderCategoryIds(); + $catIds = $this->getSliderCategoryIds(); $collection = $this->_productCollectionFactory->create(); if (is_array($catIds)) { foreach ($catIds as $catId) { diff --git a/Block/CustomProducts.php b/Block/CustomProducts.php index fde40af..03ee6f7 100644 --- a/Block/CustomProducts.php +++ b/Block/CustomProducts.php @@ -42,6 +42,7 @@ class CustomProducts extends AbstractSlider /** * CustomProducts constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -66,8 +67,7 @@ public function __construct( Configurable $configurable, LayoutFactory $layoutFactory, array $data = [] - ) - { + ) { parent::__construct( $context, diff --git a/Block/MostViewedProducts.php b/Block/MostViewedProducts.php index 41c042f..1726d80 100644 --- a/Block/MostViewedProducts.php +++ b/Block/MostViewedProducts.php @@ -46,6 +46,7 @@ class MostViewedProducts extends AbstractSlider /** * MostViewedProducts constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -72,8 +73,7 @@ public function __construct( Configurable $configurable, LayoutFactory $layoutFactory, array $data = [] - ) - { + ) { $this->_mostViewedProductsFactory = $mostViewedProductsFactory; parent::__construct( diff --git a/Block/NewProducts.php b/Block/NewProducts.php index 2526525..dfa029e 100644 --- a/Block/NewProducts.php +++ b/Block/NewProducts.php @@ -35,8 +35,8 @@ class NewProducts extends AbstractSlider public function getProductCollection() { $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds(); - $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts); - $collection = $this->_addProductAttributesAndPrices($collection) + $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts); + $collection = $this->_addProductAttributesAndPrices($collection) ->addAttributeToFilter( 'news_from_date', ['date' => true, 'to' => $this->getEndOfDayDate()], diff --git a/Block/OnSaleProduct.php b/Block/OnSaleProduct.php index 98bb872..36a3aa4 100644 --- a/Block/OnSaleProduct.php +++ b/Block/OnSaleProduct.php @@ -45,6 +45,7 @@ class OnSaleProduct extends AbstractSlider /** * OnSaleProduct constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -69,8 +70,7 @@ public function __construct( Configurable $configurable, LayoutFactory $layoutFactory, array $data = [] - ) - { + ) { $this->_dateTimeStore = $dateTime; parent::__construct( $context, @@ -111,14 +111,14 @@ public function getProductCollection() 'price_index.final_price < price_index.price' ); - $productIds = $this->getProductParentIds($productCollection); + $productIds = $this->getProductParentIds($productCollection); $productCollection = $this->_productCollectionFactory->create()->addIdFilter($productIds); $productCollection->addAttributeToFilter('visibility', ['neq' => 1]) ->addAttributeToFilter('status', 1) - ->addAttributeToSelect('*') ->addStoreFilter($this->getStoreId()) ->setPageSize($this->getProductsCount()); + $this->_addProductAttributesAndPrices($productCollection); return $productCollection; } diff --git a/Block/RecentProducts.php b/Block/RecentProducts.php index ae21200..c855738 100644 --- a/Block/RecentProducts.php +++ b/Block/RecentProducts.php @@ -46,6 +46,7 @@ class RecentProducts extends AbstractSlider /** * RecentProducts constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -72,8 +73,7 @@ public function __construct( Configurable $configurable, LayoutFactory $layoutFactory, array $data = [] - ) - { + ) { $this->reportProductViewed = $reportProductViewed; parent::__construct( diff --git a/Block/WishlistProducts.php b/Block/WishlistProducts.php index 1688295..a647f17 100644 --- a/Block/WishlistProducts.php +++ b/Block/WishlistProducts.php @@ -52,6 +52,7 @@ class WishlistProducts extends AbstractSlider /** * WishlistProducts constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -80,10 +81,9 @@ public function __construct( Configurable $configurable, LayoutFactory $layoutFactory, array $data = [] - ) - { + ) { $this->_wishlistCollectionFactory = $wishlistCollectionFactory; - $this->_customerSession = $_customerSession; + $this->_customerSession = $_customerSession; parent::__construct( $context, diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml deleted file mode 100644 index 6bc6cdd..0000000 --- a/view/frontend/layout/default.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - diff --git a/view/frontend/templates/productslider.phtml b/view/frontend/templates/productslider.phtml index e9469c2..4fab00b 100644 --- a/view/frontend/templates/productslider.phtml +++ b/view/frontend/templates/productslider.phtml @@ -117,6 +117,8 @@ if ($block->getHelperData()->isEnabled() && $items && $items->getSize()): ?> getPagerHtml() ?> + + getPagerHtml() ?> getPagerHtml() ?>