Categories: How ToMagento 2

How to get a Special Price of the Product in Magento 2

Magento 2 is one of featured pack CMS that comes with tons of preloaded configurations that makes it a perfect fit for any business. Even if you are setting up price for the product you can set 3 different kinds of prices including Group price, Special price & Tier price depending on your business needs. Several times it happens when the store owner is offering some of the store products at a discounted price for the specific period of time. During that specified time period, the special price appears instead of the regular price in the Magento 2 front, followed by a notation that shows the regular price.
Several times, it happens that while developing any payment related extension or functionality, the developer first needs to check that the product contains any special price or offered at a normal price before performing any pricing related operations. Because if any product contains a special price, then the base product price for the calculations will be special pricing, not the regular price. So, we are again back with a small piece of code that will help you to get the special price of the product in Magento 2.
Simply place below code anywhere in your extension or development to check that product contains special price or not.

Use below code to check if product have special price or not
$specialprice = $_product->getSpecialPrice();
$specialfromdate = $_product->getSpecialFromDate();
$specialtodate = $_product->getSpecialToDate();
$today = time();

   if ((is_null($specialfromdate) &&is_null($specialtodate)) || ($today >= 
    strtotime($specialfromdate) &&is_null($specialtodate)) || ($today = 
    strtotime($specialfromdate) && $today 

then you can check like below in your code.

$specialPriceExist ? $specialPriceExist : $_product->getCustomUnitAmount()*$_product->getPricePerCustomUnit()

That’s it! If you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends.
And, Let us know if you are facing an issue while implementing this code.
Happy Coding!

Click to rate this post!
[Total: 12 Average: 4.3]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago