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 ?

| improve this question | | | | |
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()
| improve this answer | | | | |
  • thx did exactly that.. – Naberd Zendi May 23 '19 at 15:05
  • 1
    Happy to help, please accept and upvote!! – jafar pinjar May 23 '19 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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