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.
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.
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");
I believe you're looking for $product->getPrice() and $product->getSpecialPrice().