How to Check if the Product has a Special Price in Magento 2
One of the most common approaches various businesses try to uplift their sales figure is offering the discount. Magento 2 store owners usually incorporate the discounts on store products by displaying the actual price along with the special price to lure the visitors to shop at discounted rates. After the implementation, the home page or category page displays the actual price as cancelled and the special price as the price to be paid.
When you create a custom page in Magento 2 for the products and assign the fetched price, it assigns the actual price instead of the special prices, which is wrong. So you have to check if the product has a special price in Magento 2 store and assign the same to the products. Here, I have come up with the code to check if the product has a special price in Magento 2.
Methods to check if the product has a special price in Magento 2:
Methods to check if the product has a special price in Magento 2:
-
With Object Manager
12345678910111213141516171819<?php$product_id = 10; //Product ID$objectManager = \Magento\Framework\App\ObjectManager::getInstance();$_product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);$orgprice = $_product->getPrice();$specialprice = $_product->getSpecialPrice();$specialfromdate = $_product->getSpecialFromDate();$specialtodate = $_product->getSpecialToDate();$today = time();if (!$specialprice)$specialprice = $orgprice;if ($specialprice< $orgprice) {if ((is_null($specialfromdate) &&is_null($specialtodate)) || ($today >= strtotime($specialfromdate) &&is_null($specialtodate)) || ($today <= strtotime($specialtodate) &&is_null($specialfromdate)) || ($today >= strtotime($specialfromdate) && $today <= strtotime($specialtodate))) {echo 'product has a special price';}}?> -
Block Method
12345678910111213141516171819202122232425262728use Magento\Catalog\Model\ProductFactory;protected $productFactory;public function __construct(ProductFactory $productFactory) {$this->productFactory = $productFactory;}public function getIsSpecialPrice($productId){$_product = $this->productFactory->create()->load($productId);$orgprice = $_product->getPrice();$specialprice = $_product->getSpecialPrice();$specialfromdate = $_product->getSpecialFromDate();$specialtodate = $_product->getSpecialToDate();$today = time();if (!$specialprice)$specialprice = $orgprice;if ($specialprice< $orgprice) {if ((is_null($specialfromdate) &&is_null($specialtodate)) || ($today >= strtotime($specialfromdate) &&is_null($specialtodate)) || ($today <= strtotime($specialtodate) &&is_null($specialfromdate)) || ($today >= strtotime($specialfromdate) && $today <= strtotime($specialtodate))) {return 1;}}return 0;}
That’s it!
Easy to implement and effective Bring the special prices and show with the products in your custom Magento 2 pages.
Once checked, you can set special prices for products in Magento 2.
You may mention any doubts in the comments section below and I’ll solve it asap!
Rate the post with 5 stars to appreciate the efforts
Happy Coding!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
As per the Magento guidelines one should not use object manager directly in the scripts. What do you think?
Yes, you are right about the use of Object Manager. Thanks for drawing my attention. I have updated the post with alternate solution that can be used.
what you do if discount on child product of configurable product?
Hello,
Please note that one can use the above code to check the special price of the product and not the discount price on the product.
Thanks.