---
title: "How to Get Special Price Product Collection In Magento 2 "
url: "https://magecomp.com/blog/get-special-price-product-collection-in-magento-2/"
date: "2021-03-24T19:14:36+00:00"
modified: "2024-11-25T13:30:40+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "Magento Tutorials"
word_count: 937
reading_time: "5 min read"
summary: "Hello Magento Folks,"
description: "Special price is configured after that there is difficulty in tracking those products. Learn How to Get Special Price Product Collection In Magento 2 "
keywords: "Magento Tutorials"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How To Configure Checkout Totals Sort Order In Magento 2?"
    url: "https://magecomp.com/blog/configure-checkout-totals-sort-order-magento-2/"
  - title: "How To Enable Admin Dashboard Charts In Magento 2"
    url: "https://magecomp.com/blog/enable-admin-dashboard-charts-magento-2/"
  - title: "How to Add Filterable Product Attribute in Products in Category in Magento 2?"
    url: "https://magecomp.com/blog/add-filterable-product-attribute-in-products-in-category-in-magento-2/"
---

# How to Get Special Price Product Collection In Magento 2 

_Published: March 24, 2021_  
_Author: Dhiren Vasoya_  

![](https://magecomp.com/blog/wp-content/uploads/2021/03/How-to-Get-Special-Price-Product-Collection-In-Magento-2-png.webp)

**Hello Magento Folks,**

In this article, I will help you to learn How To Get Special Price Product Collection In Magento 2. Also, check out our latest post where I have illustrated [**How to add Custom Tab in Customer Account in Magento 2.** ](https://magecomp.com/blog/add-custom-tab-in-customer-account-in-magento-2/)

Many times when the store admin sets discounts for their Magento 2 store if you are that person then this article is very helpful for you. Basically, when the special price is configured after that there is difficulty in tracking those products. Sometimes the admin will require this data for analyzing the market and finding the change in the behavior of customers when a special price is offered. When this task is performed manually then there will be a waste of time and it will be a difficult task. There are also chances of mistakes therefore ignore all these tactics and apply the below-given steps to Get Special Price Product Collection In Magento 2.
## Steps to Get Special Price Product Collection In Magento 2:
**Step 1:** Firstly, Create a **routes.xml** file at the given below path and add code to it.
**app\code\Vendor\Extension\etc\frontend\**

```
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route frontName="routesname" id="routesname">
            <module name="Vendor_Extension"/>
        </route>
    </router>
</config>
```

**Step 2:** Create **routesname_product_index.xml** file at the given below path and add code to it.
**app\code\Vendor\Extension\view\frontend\layout\**

```
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="VendorExtensionBlockGetproductspecialprice" name="custom_tab" template="Vendor_Extension::specialpricedata.phtml" />
        </referenceContainer>
    </body>
</page>
```

**Step 3:** Create **Index.php** file at the given below path and add code to it.
**app\code\Vendor\Extension\Controller\product\**

```
<?php
namespace VendorExtensionControllerCustomer;

class Index extends MagentoFrameworkAppActionAction
{
    protected $_pageFactory;
    public function __construct(
        MagentoFrameworkAppActionContext $context,
        MagentoFrameworkViewResultPageFactory $pageFactory)
    {
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
    }
}
```

**Step 4:** Create the **Getproductspecialprice.php** file at the given below path and add code to it.
**app\code\Vendor\Extension\Block\**

```
<?php
namespace VendorExtensionBlock;

use MagentoCatalogApiCategoryRepositoryInterface;
use MagentoCatalogBlockProductContext;
use MagentoFrameworkDataHelperPostHelper;
use MagentoCatalogModelLayerResolver;
use MagentoFrameworkUrlHelperData;
use MagentoCatalogModelResourceModelProductCollection;
use MagentoFrameworkAppResourceConnection;
class Getproductspecialprice extends MagentoCatalogBlockProductListProduct
{
    protected $_collection;
    protected $categoryRepository;
    protected $_resource;

    public function __construct(Context $context,
                                PostHelper $postDataHelper,
                                Resolver $layerResolver,
                                CategoryRepositoryInterface $categoryRepository,
                                Data $urlHelper,
                                Collection $collection,
                                ResourceConnection $resource,
                                array $data = []
    )
    {
        $this->categoryRepository = $categoryRepository;
        $this->_collection = $collection;
        $this->_resource = $resource;
        parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
    }

    public function getproductcollection()
    {
        $count = 10;
        $category_id = $this->getData("category_id"); // pass your category id
        $collection = clone $this->_collection;
        $collection->clear()->getSelect()->reset(MagentoFrameworkDBSelect::WHERE)->reset(MagentoFrameworkDBSelect::ORDER)->reset(MagentoFrameworkDBSelect::LIMIT_COUNT)->reset(MagentoFrameworkDBSelect::LIMIT_OFFSET)->reset(MagentoFrameworkDBSelect::GROUP);
        if (!$category_id)
       {
            $category_id = $this->_storeManager->getStore()->getRootCategoryId();
        }
        $categorycollection = $this->categoryRepository->get($category_id);
        $today_date = date('Y-m-d');
        if (isset($categorycollection) && $categorycollection)
        {
            $collection->addMinimalPrice()->addFinalPrice()->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addAttributeToSelect('special_from_date')
                ->addAttributeToSelect('special_to_date')
                ->addAttributeToFilter('special_price', ['neq' => ''])
                ->addAttributeToFilter([
                    [
                        'attribute' => 'special_from_date',
                        'lteq' => date('Y-m-d G:i:s', strtotime($today_date)),
                        'date' => true,
                    ],
                    [
                        'attribute' => 'special_to_date',
                        'gteq' => date('Y-m-d G:i:s', strtotime($today_date)),
                        'date' => true,
                    ]
                ])
                ->addAttributeToFilter('is_saleable', 1, 'left');
        }
        else
        {
            $collection->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addAttributeToFilter('special_price', ['neq' => ''])
                ->addAttributeToSelect('special_from_date')
                ->addAttributeToSelect('special_to_date')
                ->addAttributeToFilter([
                    [
                        'attribute' => 'special_from_date',
                        'lteq' => date('Y-m-d G:i:s', strtotime($today_date)),
                        'date' => true,
                    ],
                    [
                        'attribute' => 'special_to_date',
                        'gteq' => date('Y-m-d G:i:s', strtotime($today_date)),
                        'date' => true,
                    ]
                ])
                ->addAttributeToFilter('is_saleable', 1, 'left');
        }
        $collection->getSelect()->limit($count);
        return $collection;
    }
}
```

**Step 5:** Create a **specialpricedata.phtml** file at the given below path add code to it.
**app\code\Vendor\Extension\view\frontend\templates**

```
<?php foreach ($block->getproductcollection() as $product)
{
    echo '';
    //print_r($product->getSpecialPrice()); 	// this code for getSpecialPrice for products
    print_r($product->getData()); 		// this line get all getSpecialPrice products collection
    echo '';
}
```

## Wrap Up:
Hopefully, all are able to Get Special Price Product Collection In Magento 2 by implementing the above-given steps. If you find any difficulties in following the above steps then connect with me in the comment section below. You can also take the help of our [**Certified Magento developers**](https://magecomp.com/hire-magento-developer.html) to help you in getting a special price product collection in Magento 2.
Share the article with your Magento Store Admin friends.
**Happy Reading.**

</body>


---

_View the original post at: [https://magecomp.com/blog/get-special-price-product-collection-in-magento-2/](https://magecomp.com/blog/get-special-price-product-collection-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-12 15:47:10 UTC_  
