13

How can I know if the product is currently having a discount?

I used this code.

if($product->getFinalPrice() < $product->getPrice()){
   //had a discount
}

But it doesn't works.

1
  • I think you need special price ?
    – Keyul Shah
    Commented Jan 15, 2014 at 3:36

2 Answers 2

13

The code you mentioned always worked for me. I think it depends how you get the $product.
If you do this it should work.

$product = Mage::getModel('catalog/product')->load($id);

if you get the products from a collection, get the collection like this:

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents() //additional filters go here;

Now you can loop through the collection and do your check.

foreach ($collection as $product){
    if($product->getFinalPrice() < $product->getPrice()){
       //had a discount
    }
}

This method takes into consideration the discount provided by special prices and catalog price rules.

Additional info. A bit off topic but useful: Here is how you can get the list of products that have a discount

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addUrlRewrite();

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

$collection->getSelect()->where("`price_index`.price !=price_index.min_price");
5

I believe you're looking for $product->getPrice() and $product->getSpecialPrice().

1
  • 2
    This, however will not detect if the product is on discount via a catalog price rule.
    – ProxiBlue
    Commented Feb 20, 2015 at 2:52

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.