1

I have created one countdown timer for special price, now i want to hide timer if product does not have any special price so how can i do?Please help me.

4

Using \Magento\Catalog\Model\ProductRepository

protected $_productRepository;

public function __construct(
    ...      
    \Magento\Catalog\Model\ProductRepository $productRepository,
    ...
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($id)
{
    //$id = '21'; //Product ID
    $product = $this->_productRepository->getById($id);
    return $product->getSpecialPrice();
}

public function getSpecialPriceBySku($sku)
{   
    //$sku = 'test-21'; //Product Sku
    $product = $this->_productRepository->get($sku);
    return $product->getSpecialPrice();
}

Now you can get this via

$this->getSpecialPriceById($id);
$this->getSpecialPriceBySku($sku);
0

You can use this \Magento\Catalog\Model\ProductRepository class to get the special price of the product

protected $_productRepository;

public function __construct(
    ...      
    \Magento\Catalog\Model\ProductRepository $productRepository,
    ...
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($productId)
{
    $product = $this->_productRepository->getById($productId);
    return $product->getSpecialPrice();
}

Now you can get the special price of product by just passing the product id:

$this->getSpecialPriceById($productId);

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.