Question

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?

Was it helpful?

Solution

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';
    }
}
 
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top