diff --git a/Block/AbstractSlider.php b/Block/AbstractSlider.php index c542073..b5ba9d7 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,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; @@ -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. @@ -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( @@ -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 * @@ -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; } /** @@ -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), ] ]; @@ -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; } /** @@ -242,10 +254,10 @@ 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']) @@ -253,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, @@ -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); } /** @@ -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') { @@ -445,6 +465,11 @@ public function getIdentities() return $identities ?: [Product::CACHE_TAG]; } + /** + * @return mixed + */ + abstract public function getProductCollection(); + /** * Get Product Count is displayed * @@ -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; + } } diff --git a/Block/BestSellerProducts.php b/Block/BestSellerProducts.php index 9807862..cbe3320 100644 --- a/Block/BestSellerProducts.php +++ b/Block/BestSellerProducts.php @@ -24,9 +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\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; @@ -52,6 +55,9 @@ class BestSellerProducts extends AbstractSlider * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder * @param BestSellersCollectionFactory $bestSellersCollectionFactory + * @param Grouped $grouped + * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -63,6 +69,9 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, BestSellersCollectionFactory $bestSellersCollectionFactory, + Grouped $grouped, + Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { $this->_bestSellersCollectionFactory = $bestSellersCollectionFactory; @@ -75,6 +84,9 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, + $layoutFactory, $data ); } @@ -85,16 +97,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..dc0527f 100644 --- a/Block/CategoryId.php +++ b/Block/CategoryId.php @@ -25,9 +25,12 @@ 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\Framework\View\LayoutFactory; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Mageplaza\Productslider\Helper\Data; /** @@ -52,6 +55,9 @@ class CategoryId extends AbstractSlider * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder * @param CategoryFactory $categoryFactory + * @param Grouped $grouped + * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -63,6 +69,9 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, CategoryFactory $categoryFactory, + Grouped $grouped, + Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { $this->_categoryFactory = $categoryFactory; @@ -75,6 +84,9 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, + $layoutFactory, $data ); } @@ -90,8 +102,7 @@ public function getProductCollection() $collection = []; if (!empty($productIds)) { $collection = $this->_productCollectionFactory->create() - ->addIdFilter($productIds) - ->setPageSize($this->getProductsCount()); + ->addIdFilter(array('in' => $productIds)); $this->_addProductAttributesAndPrices($collection); } @@ -106,23 +117,46 @@ public function getProductCollection() public function getProductIdsByCategory() { $productIds = []; - $catIds = $this->getSliderCategoryIds(); - $collection = $this->_productCollectionFactory->create(); + $catIds = $this->getSliderCategoryIds(); + if (is_array($catIds)) { - foreach ($catIds as $catId) { - $category = $this->_categoryFactory->create()->load($catId); + $productId = []; + + foreach($catIds as $cat) + { + $collection = $this->_productCollectionFactory->create(); + $category = $this->_categoryFactory->create()->load($cat); $collection->addAttributeToSelect('*')->addCategoryFilter($category); + + foreach ($collection as $item) { + $productId[] = $item->getData('entity_id'); + } + + $productIds = array_merge($productIds, $productId); } - } else { + }else { + $collection = $this->_productCollectionFactory->create(); $category = $this->_categoryFactory->create()->load($catIds); $collection->addAttributeToSelect('*')->addCategoryFilter($category); + + foreach ($collection as $item) { + $productIds[] = $item->getData('entity_id'); + } } - foreach ($collection as $item) { - $productIds[] = $item->getData('entity_id'); + $keys = array_keys($productIds); + shuffle($keys); + $productIdsRandom = []; + + foreach ($keys as $key => $value) { + $productIdsRandom[] = $productIds[$value]; + + if ($key >= ($this->getProductsCount() - 1)) { + break; + } } - return $productIds; + return $productIdsRandom; } /** diff --git a/Block/CustomProducts.php b/Block/CustomProducts.php index aacb6f6..cfd9a89 100644 --- a/Block/CustomProducts.php +++ b/Block/CustomProducts.php @@ -21,18 +21,76 @@ 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\Framework\View\LayoutFactory; +use Magento\GroupedProduct\Model\Product\Type\Grouped; +use Mageplaza\Productslider\Helper\Data; + + /** * Class CustomProducts * @package Mageplaza\Productslider\Block */ class CustomProducts extends AbstractSlider { + + /** + * 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 LayoutFactory $layoutFactory + * @param array $data + */ + public function __construct( + Context $context, + CollectionFactory $productCollectionFactory, + Visibility $catalogProductVisibility, + DateTime $dateTime, + Data $helperData, + HttpContext $httpContext, + EncoderInterface $urlEncoder, + Grouped $grouped, + Configurable $configurable, + LayoutFactory $layoutFactory, + array $data = [] + ) { + + parent::__construct( + $context, + $productCollectionFactory, + $catalogProductVisibility, + $dateTime, + $helperData, + $httpContext, + $urlEncoder, + $grouped, + $configurable, + $layoutFactory, + $data + ); + } + /** - * @return $this|mixed + * @return \Magento\Catalog\Model\ResourceModel\Product\Collection|mixed|null */ public function getProductCollection() { $productIds = $this->getSlider()->getProductIds(); + $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds(); if (!is_array($productIds)) { $productIds = explode('&', $productIds); } @@ -44,7 +102,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)->setVisibility($visibleProducts); + $collection->addMinimalPrice() + ->addFinalPrice() + ->addTaxPercents() + ->addAttributeToSelect('*') + ->addStoreFilter($this->getStoreId())->setPageSize($this->getProductsCount()); return $collection; } diff --git a/Block/MostViewedProducts.php b/Block/MostViewedProducts.php index 2c30c57..1726d80 100644 --- a/Block/MostViewedProducts.php +++ b/Block/MostViewedProducts.php @@ -24,9 +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\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; @@ -50,7 +53,11 @@ class MostViewedProducts extends AbstractSlider * @param DateTime $dateTime * @param Data $helperData * @param HttpContext $httpContext + * @param EncoderInterface $urlEncoder * @param MostViewedCollectionFactory $mostViewedProductsFactory + * @param Grouped $grouped + * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -62,6 +69,9 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, MostViewedCollectionFactory $mostViewedProductsFactory, + Grouped $grouped, + Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { $this->_mostViewedProductsFactory = $mostViewedProductsFactory; @@ -74,6 +84,9 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, + $layoutFactory, $data ); } @@ -88,7 +101,15 @@ public function getProductCollection() ->setStoreId($this->getStoreId())->addViewsCount() ->addStoreFilter($this->getStoreId()) ->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/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 ff5c48a..36a3aa4 100644 --- a/Block/OnSaleProduct.php +++ b/Block/OnSaleProduct.php @@ -24,9 +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\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; /** @@ -42,6 +45,7 @@ class OnSaleProduct extends AbstractSlider /** * OnSaleProduct constructor. + * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -49,6 +53,9 @@ class OnSaleProduct extends AbstractSlider * @param Data $helperData * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder + * @param Grouped $grouped + * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -59,6 +66,9 @@ public function __construct( Data $helperData, HttpContext $httpContext, EncoderInterface $urlEncoder, + Grouped $grouped, + Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { $this->_dateTimeStore = $dateTime; @@ -70,6 +80,9 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, + $layoutFactory, $data ); } @@ -79,64 +92,34 @@ 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()) + ->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' + ); + + $productIds = $this->getProductParentIds($productCollection); + $productCollection = $this->_productCollectionFactory->create()->addIdFilter($productIds); + $productCollection->addAttributeToFilter('visibility', ['neq' => 1]) + ->addAttributeToFilter('status', 1) + ->addStoreFilter($this->getStoreId()) + ->setPageSize($this->getProductsCount()); + $this->_addProductAttributesAndPrices($productCollection); - return $collectionClone; + return $productCollection; } } diff --git a/Block/RecentProducts.php b/Block/RecentProducts.php index 3fff255..c855738 100644 --- a/Block/RecentProducts.php +++ b/Block/RecentProducts.php @@ -24,9 +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\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; @@ -52,6 +55,9 @@ class RecentProducts extends AbstractSlider * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder * @param ReportProductViewed $reportProductViewed + * @param Grouped $grouped + * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -63,6 +69,9 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, ReportProductViewed $reportProductViewed, + Grouped $grouped, + Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { $this->reportProductViewed = $reportProductViewed; @@ -75,6 +84,9 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, + $layoutFactory, $data ); } diff --git a/Block/Widget/Slider.php b/Block/Widget/Slider.php index 7b34873..8cf340a 100644 --- a/Block/Widget/Slider.php +++ b/Block/Widget/Slider.php @@ -24,10 +24,13 @@ 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; @@ -52,7 +55,6 @@ class Slider extends AbstractSlider /** * Slider constructor. - * * @param Context $context * @param CollectionFactory $productCollectionFactory * @param Visibility $catalogProductVisibility @@ -61,6 +63,9 @@ class Slider extends AbstractSlider * @param HttpContext $httpContext * @param EncoderInterface $urlEncoder * @param ProductType $productType + * @param Grouped $grouped + * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -72,8 +77,12 @@ public function __construct( HttpContext $httpContext, EncoderInterface $urlEncoder, ProductType $productType, + Grouped $grouped, + Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] - ) { + ) + { parent::__construct( $context, $productCollectionFactory, @@ -82,6 +91,9 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, + $layoutFactory, $data ); $this->productType = $productType; @@ -118,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 * @@ -164,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 67e8775..a647f17 100644 --- a/Block/WishlistProducts.php +++ b/Block/WishlistProducts.php @@ -24,10 +24,13 @@ 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\Framework\View\LayoutFactory; +use Magento\GroupedProduct\Model\Product\Type\Grouped; use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistCollectionFactory; use Mageplaza\Productslider\Helper\Data; @@ -59,6 +62,9 @@ class WishlistProducts extends AbstractSlider * @param EncoderInterface $urlEncoder * @param WishlistCollectionFactory $wishlistCollectionFactory * @param CustomerSession $_customerSession + * @param Grouped $grouped + * @param Configurable $configurable + * @param LayoutFactory $layoutFactory * @param array $data */ public function __construct( @@ -71,10 +77,13 @@ public function __construct( EncoderInterface $urlEncoder, WishlistCollectionFactory $wishlistCollectionFactory, CustomerSession $_customerSession, + Grouped $grouped, + Configurable $configurable, + LayoutFactory $layoutFactory, array $data = [] ) { $this->_wishlistCollectionFactory = $wishlistCollectionFactory; - $this->_customerSession = $_customerSession; + $this->_customerSession = $_customerSession; parent::__construct( $context, @@ -84,6 +93,9 @@ public function __construct( $helperData, $httpContext, $urlEncoder, + $grouped, + $configurable, + $layoutFactory, $data ); } @@ -98,12 +110,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()); 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, diff --git a/composer.json b/composer.json index 69888a7..bb02952 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "mageplaza/module-core": "^1.4.5" }, "type": "magento2-module", - "version": "4.0.0", + "version": "4.0.1", "license": "proprietary", "authors": [ { diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml index 225c787..f387668 100644 --- a/view/frontend/layout/default.xml +++ b/view/frontend/layout/default.xml @@ -1,28 +1,28 @@ - - - - - - - - + + + + + + + + diff --git a/view/frontend/templates/productslider.phtml b/view/frontend/templates/productslider.phtml index e9469c2..6959b88 100644 --- a/view/frontend/templates/productslider.phtml +++ b/view/frontend/templates/productslider.phtml @@ -23,6 +23,7 @@ use Mageplaza\Productslider\Block\AbstractSlider; use Magento\Framework\App\Action\Action; /** @var AbstractSlider $block */ +$helper = $this->helper(\Mageplaza\Productslider\Helper\Data::class); $mode = 'grid'; $type = 'widget-product-grid'; $title = $block->getTitle(); @@ -115,6 +116,20 @@ if ($block->getHelperData()->isEnabled() && $items && $items->getSize()): ?> + getPagerHtml() ?> getPagerHtml() ?>