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:
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* 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 (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
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)
->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;
}
}
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.
<?php
$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.
Collection
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.
- How to create a simple Hello World module for Magento 2
- Magento 2 Block Template Ultimate Guides
- How to Create Module in Magento 2
- How to Create Controller in Magento 2
- How to create CRUD Models in Magento 2
- How to Create Magento 2 Block, Layout and Templates
- Configuration - System.xml
- How To Create Admin Menu In Magento 2
- Admin ACL
- Admin Grid