0

I want to show all the on sale product on one page. Can any one help me with the code to get the products collection which are on Sale i.e which has special price assigned in magento 2

This is my block code

<?php

 namespace Vidushi\Contact\Block;

 use Zend_Db_Expr;

 class OnSaleProduct extends AbstractSlider
   {
     /**
       * @inheritdoc
    */
  public function getProductCollection()
  {
      $visibleProducts = $this->_catalogProductVisibility- 
     >getVisibleInCatalogIds();
    $collection = $this->_productCollectionFactory->create()- 
>setVisibility($visibleProducts);
    $collection = $this->_addProductAttributesAndPrices($collection)
        ->addAttributeToFilter(
            'special_from_date',
            ['date' => true, 'to' => $this->getEndOfDayDate()],
            'left'
        )->addAttributeToFilter(
            'special_to_date',
            ['or' => [0 => ['date' => true,
                                               'from' => $this- 
  >getStartOfDayDate()],
                                         1 => ['is' => new Zend_Db_Expr(
                                             'null'
                                         )],]],
            'left'
        )->addAttributeToSort(
            'news_from_date',
            'desc'
        );
    return $collection;
}
   
}

3 Answers 3

0

Try This :-

<?php

namespace Vendor\Module\Block;

use Zend_Db_Expr;

class OnSaleProduct extends AbstractSlider
{
    /**
     * @inheritdoc
     */
    public function getProductCollection()
    {
        $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds();
        $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts);
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addAttributeToFilter(
                'special_from_date',
                ['date' => true, 'to' => $this->getEndOfDayDate()],
                'left'
            )->addAttributeToFilter(
                'special_to_date',
                ['or' => [0 => ['date' => true,
                                                   'from' => $this->getStartOfDayDate()],
                                             1 => ['is' => new Zend_Db_Expr(
                                                 'null'
                                             )],]],
                'left'
            )->addAttributeToSort(
                'news_from_date',
                'desc'
            );
        return $collection;
    }
}
9
  • I tried this, but its not working
    – Nafsss
    Commented Jan 4, 2021 at 6:22
  • any error ?????? Commented Jan 4, 2021 at 6:22
  • Error filtering template: Warning: Invalid argument supplied for foreach() in /home/auto1/public_html/app/code/Vidushi/Contact/view/frontend/templates/list.phtml on line 4
    – Nafsss
    Commented Jan 4, 2021 at 6:26
  • Collection is coming empty i gueess
    – Nafsss
    Commented Jan 4, 2021 at 6:26
  • please attach your code Commented Jan 4, 2021 at 6:27
0
<?php
namespace Vendor\Extension\Block;
use Zend_Db_Expr;

class OnSaleProduct extends \Magento\Framework\View\Element\Template
{
   protected $_catalogProductVisibility;
   protected $_productCollectionFactory;

   public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Visibility $productVisibility,
    array $data = []
) {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_catalogProductVisibility = $productVisibility;
    parent::__construct($context, $data);
}

    public function getProductCollection()
    {
        $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds();
        $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts);
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addAttributeToFilter(
                'special_from_date',
                ['date' => true, 'to' => $this->getEndOfDayDate()],
                'left'
            )->addAttributeToFilter('special_to_date',
                ['or' => [0 => ['date' => true,
                'from' => $this->getStartOfDayDate()],
                 1 => ['is' => new Zend_Db_Expr('null')],]],
                'left'
            )->addAttributeToSort('news_from_date','desc'
            )->addStoreFilter($this->getStoreId())->setPageSize(
                $this->getProductsCount()
            );
        return $collection;
    }
}
0

Try the below solution,

<?php

namespace Vidushi\Contact\Block;

class OnSaleProduct  extends \Magento\Framework\View\Element\Template
{

    protected $_productCollectionFactory;
    protected $productVisibility;
    protected $productStatus;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
        \Magento\Catalog\Model\Product\Visibility $productVisibility,
        array $data = []
    )
    {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;;
        parent::__construct($context, $data);
    }

    public function getProductCollection()
    {
        $price_from=""; //add here the starting date
        $price_to = ""; //add here the ending date
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*')
            ->addPriceDataFieldFilter('%s >= %s', ['special_price', $price_from])
            ->addPriceDataFieldFilter('%s <= %s', ['special_price', $price_to])
            ->addSpecialPrice()
            ->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
            ->setVisibility($this->productVisibility->getVisibleInSiteIds())
            ->setPageSize(5);

            return $collection;

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.