{"id":864,"date":"2020-03-06T08:53:38","date_gmt":"2020-03-06T08:53:38","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/06\/get-special-price-product-collection-in-magento-2\/"},"modified":"2025-05-22T14:50:10","modified_gmt":"2025-05-22T09:20:10","slug":"get-special-price-product-collection-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-special-price-product-collection-in-magento-2\/","title":{"rendered":"How To Get Special Price Product Collection In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The post shows the programmatic method to&nbsp;<em><strong>get special price product collection in Magento 2<\/strong><\/em>&nbsp;store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are an admin and is responsible to set the discounts and coupons for the store, this solution is going to be handy. Ever happened that you configured a special price for the products and after some time lost the track of it?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, it may happen that you need the list of all discounts offered in the Magento 2 store for the purpose of market study. You may want to find a change in customer behavior for purchase with respect to the special prices.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, studying special prices products is essential while calculating the profits! Manually listing the products with special prices is a tedious task and prone to human error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid it, implement the below code to get a special price product collection in Magento 2. If you do not want the collection but simply check&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/check-if-product-has-a-special-price-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">if the product has a special price in Magento 2<\/a>, refer to the earlier posted solution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Get Special Price Product Collection In Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>SpecialPrice.php<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/code\/[Vendor]\/[Module]\/Block<\/strong>&nbsp;Folder add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vendor]\\[Module]\\Block;\nuse Magento\\Catalog\\Api\\CategoryRepositoryInterface;\nuse Magento\\Catalog\\Block\\Product\\Context;\nuse Magento\\Catalog\\Block\\Product\\ListProduct;\nuse Magento\\Framework\\Data\\Helper\\PostHelper;\nuse Magento\\Framework\\Url\\Helper\\Data;\nuse Magento\\Catalog\\Model\\ResourceModel\\Product\\Collection;\nuse Magento\\Framework\\App\\ResourceConnection;\nclass SpecialPrice extends ListProduct\n{\n    protected $collection;\n    protected $categoryRepository;\n    protected $resource;\n    public function __construct(Context $context, PostHelper $postDataHelper,\n        Resolver $layerResolver, CategoryRepositoryInterface $categoryRepository,\n        Data $urlHelper, Collection $collection, ResourceConnection $resource,\n        array $data = [])\n    {\n        $this->categoryRepository = $categoryRepository;\n        $this->collection = $collection;\n        $this->resource = $resource;\n        parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);\n    }\n    public function getProducts()\n    {\n        $count = $this->getProductCount();\n        $category_id = $this->getData(\"category_id\");\n        $collection = clone $this->collection;\n        $collection ->clear() ->getSelect() ->reset(\\Magento\\Framework\\DB\\Select::WHERE)->reset(\\Magento\\Framework\\DB\\Select::ORDER)\n            ->reset(\\Magento\\Framework\\DB\\Select::LIMIT_COUNT)\n            ->reset(\\Magento\\Framework\\DB\\Select::LIMIT_OFFSET)\n            ->reset(\\Magento\\Framework\\DB\\Select::GROUP);\n        if (!$category_id) \n        {\n            $category_id = $this->_storeManager->getStore()->getRootCategoryId();\n        }\n        $category = $this->categoryRepository->get($category_id);\n        $now = date('Y-m-d');\n        if (isset($category) &amp;&amp; $category) \n        {\n            $collection->addMinimalPrice() ->addFinalPrice()\n                ->addTaxPercents()->addAttributeToSelect('name')\n                ->addAttributeToSelect('image')->addAttributeToSelect('small_image')\n                ->addAttributeToSelect('thumbnail')->addAttributeToSelect('special_from_date')\n                ->addAttributeToSelect('special_to_date')->addAttributeToFilter('special_price', ['neq' => ''])\n                ->addAttributeToFilter([['attribute' => 'special_from_date',\n                'lteq' => date('Y-m-d G:i:s', strtotime($now)),\n                'date' => true, ], \n                    ['attribute' => 'special_to_date',\n                    'gteq' => date('Y-m-d G:i:s', strtotime($now)), 'date' => true,]])\n                    ->addAttributeToFilter('is_saleable', 1, 'left');\n        } \n        else \n        {\n            $collection->addMinimalPrice() ->addFinalPrice()\n                ->addTaxPercents() ->addAttributeToSelect('name')\n                ->addAttributeToSelect('image') ->addAttributeToSelect('small_image')\n                ->addAttributeToSelect('thumbnail') ->addAttributeToFilter('special_price', ['neq' => ''])\n                ->addAttributeToSelect('special_from_date')\n                ->addAttributeToSelect('special_to_date')\n                ->addAttributeToFilter([['attribute' => 'special_from_date',\n                'lteq' => date('Y-m-d G:i:s', strtotime($now)),\n                'date' => true, ], ['attribute' => 'special_to_date',\n                'gteq' => date('Y-m-d G:i:s', strtotime($now)),\n                'date' => true,]])\n                ->addAttributeToFilter('is_saleable', 1, 'left');\n        }\n        $collection->getSelect() ->limit($count);\n        return $collection;\n    }\n    public function getLoadedProductCollection()\n    {\n        return $this->getProducts();\n    }\n    public function getProductCount()\n    {\n        $limit = $this->getData(\"product_count\");\n        if (!$limit)\n            $limit = 10;\n        return $limit;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use Block as per your requirement in product listing page<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The solution was helpful to you? Do share the same with fellow Magento developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The post shows the programmatic method to&nbsp;get special price product collection in Magento 2&nbsp;store. If you are an admin and is responsible to set the&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-864","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"predecessor-version":[{"id":15048,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/864\/revisions\/15048"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}