Magento, Magento 2

How to check if the product is on sale in Magento 2

How to check if the product is on sale in Magento 2

In this tutorial, Today I will explain you about how to check if product is on sale in Magento 2. In other words, you can say that how to check if product has special price in Magento 2. When you have to collect information about which products on sale in your store at that time, you need to check that in which products special price set.

For that, how can we check that if product has set special price or not. So, let’s check that below steps to check product has special price or not.

You may also like this :

Object Manager Method :

<?php
$productId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create(\Magento\Catalog\Model\Product::class)->load($productId);

$price = $product->getPrice();
$specialPrice = $product->getSpecialPrice();
$specialFromDate = $product->getSpecialFromDate();
$specialToDate = $product->getSpecialToDate();
$today = time();
if (!$specialPrice)
{
    $specialPrice = $price;
    if ($specialPrice < $price) {
      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 __('Yes. Product is on sale. Product special price is : '.$specialPrice);
      } else {
        echo __('No. Product sale has expired.');
      }
    }
} else {
    echo __('No. Product has not special price.');
}

Dependency Injection Method :

protected $productFactory;
 
public function __construct(
    ....
    \Magento\Catalog\Model\ProductFactory $productFactory
    ....
) {
    ....
    $this->productFactory = $productFactory;
    ....
}
 
public function checkSpecialPriceByProId($productId)
{
    $product = $this->productFactory->create()->load($productId);
    $orgprice = $product->getPrice();
    $specialprice = $product->getSpecialPrice();
    $specialfromdate = $product->getSpecialFromDate();
    $specialtodate = $product->getSpecialToDate();
    if (!$specialPrice)
    {
        $specialPrice = $price;
        if ($specialPrice < $price) {
          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 __('Yes. Product is on sale. Product special price is : '.$specialPrice);
          } else {
            echo __('No. Product sale has expired.');
          }
        }
    } else {
        echo __('No. Product has not special price.');
    }
}

You can use both method for check product has special price or not. Using dependecy injection you can inject class in construct and then, you can call checkSpecialPriceByProId($productId) method with pass product ID to retrieve output.

That’s it !!!

I hope this blog is easy to understand about how to check if product is on sale in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged ,