1

i have a function which sets the correct price for calculation:

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

if we have special price set it should return special price and otherwise normal product price based on calculation.

however special price from and to dates are way in the past and $_product->getSpecialPrice() returns original price back instead of null / false

is there a correct way to check if product has special price set or not ?

or it's a magento 2 bug ?

1 Answer 1

4

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($specialtodate) &&is_null($specialfromdate)) || ($today >= 
    strtotime($specialfromdate) && $today <= strtotime($specialtodate))) {
    $specialPriceExist = true;
   }else{
     $specialPriceExist = false;
   }

then you can check like below in your code

$specialPriceExist ? $specialPriceExist : $_product->getCustomUnitAmount()*$_product->getPricePerCustomUnit()
2
  • thx did exactly that.. May 23, 2019 at 15:05
  • 1
    Happy to help, please accept and upvote!! May 23, 2019 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.