How to Get On Sale Products Collection

In an online store, it’s essential to show products on sale on the homepage, or any page to notice customers and make it good for sales.

In order to display On the Sale Products collection on the site, the store admin will need the data about On Sale Product Collection. However, getting the collection is not a simple task, and there are also not many articles that write about the solution for this. Therefore, in today post, I will guide you on how to get On Sale Product Collection in Magento 2.

3 Steps to get On Sale Product Collection

Step 1: Create OnSaleProduct block {}

To get On Sale Product Collection, firstly, you need to create a OnSaleProduct block. To do that, follow the path Mageplaza/Productslider/Block/OnSaleProduct.php and add the below code:

 * Mageplaza
 * This source file is subject to the license that is
 * available through the world-wide-web at this URL:
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 * @category    Mageplaza
 * @package     Mageplaza_Productslider
 * @copyright   Copyright (c) Mageplaza (
 * @license
namespace Mageplaza\Productslider\Block;
use Zend_Db_Expr;
 * Class OnSaleProduct
 * @package Mageplaza\Productslider\Block
class OnSaleProduct extends AbstractSlider
     * @inheritdoc
    public function getProductCollection()
        $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds();
        $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts);
        $collection = $this->_addProductAttributesAndPrices($collection)
                ['date' => true, 'to' => $this->getEndOfDayDate()],
                ['or' => [0 => ['date' => true,
                                                   'from' => $this->getStartOfDayDate()],
                                             1 => ['is' => new Zend_Db_Expr(
        return $collection;

Step 2: Insert in phtml file {}

After having the collection in the block, now you can follow this snippet to get product collection from the block Mageplaza/HelloWorld/view/frontend/templates/list.phtml

Then, please insert the following code in the phtml file.

$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';

Step 3: Flush Cache & Test result {}

Finally, let’s flush cache and test result.


Above are three steps to help you get On the Sale Products Collection in Magento 2. I hope after reading this, you will be able to display your collection easily. If you have any questions or new ideas, feel free to leave a comment below.

Image Description
Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.