0

I need get special price my Magento 2 product, I can get price via:

$priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); 
echo $priceHelper->currency($product->getFinalPrice(), true, false);

but I can't get special price, I have block:

<p class="special-price">
                          <meta itemprop="priceValidUntil" content="2016-09-30 00:00:00">
                          <span class="price-label">Special Price</span>
                          <span class="price" id="product-price-<?php echo $id ?>">
                          <?php echo $product->getSpecialPrice();  ?> 
                          </span>
                        </p>

in content meta I need get date until ended special price and getSpecialPrice, but this won't working properly. Where my mistake?

1 Answer 1

1

Check Product have special price in Magento 2.

<?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';
    }
}
 
?>

Your Answer

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

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