Magento – Display Sale icon on special price products in magento. Only at homepage it is showing not in all pages

magento-1.7php-5.4productproduct-list

Display Sale icon on special price products in magento. Only at homepage it is showing not in all pages.

Code I have edited in
app/design/frontend/default/template/catalog/product/list.html

from the line number: 95

    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<?php 
    // Get the Special Price
    $specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice(); 
    // Get the Special Price FROM date
    $specialPriceFromDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialFromDate();
    // Get the Special Price TO date
    $specialPriceToDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialToDate();
    // Get Current date
    $today =  time();
 
    if ($specialprice):
        if($today >= strtotime( $specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime( $specialPriceFromDate) && is_null($specialPriceToDate)):
 ?>
    <img src="../images/sale-icon.png" width="101" height="58" class="onsaleicon" />
<?php  
        endif;
    endif;
?>     
</a>

This icon is displaying in homepage only not in All product list of pages. How do i display in all pages?

Best Answer

This could be the problem:

<img src="../images/sale-icon.png" width="101" height="58" class="onsaleicon" />

When you are not on homepage the relative path to the image is wrong.
Use this instead:

<img src="<?php echo $this->getSkinUrl('images/sale-icon.png')?>" width="101" height="58" class="onsaleicon" />

and place your image in skin/frontend/{interface}/{theme}/images/.

Small tip:
Don't use this:

$specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice(); 
    // Get the Special Price FROM date
    $specialPriceFromDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialFromDate();
    // Get the Special Price TO date
    $specialPriceToDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialToDate();

You are loading the product 3 times and you don't need to do that. You can load it only once if neccesary:

$mainProduct = Mage::getModel('catalog/product')->load($_product->getId());
$specialprice = $mainProduct->getSpecialPrice(); 
// Get the Special Price FROM date
$specialPriceFromDate = $mainProduct->getSpecialFromDate();
// Get the Special Price TO date
$specialPriceToDate = $mainProduct->getSpecialToDate();

but you can go without loading it, by editing the attributes 'special_price', 'special_from_date' andspecial_to_date` and settingused in product listingtoyes`.

bigger tip:

You can check if the product has a discount without checking the special price and the dates. This works even if you have a discount from a catalog rule:

if ($_product->getPrice() > $_product->getFinalPrice())
Related Topic