Question

I am trying to display a sale badge over the image of products with a special price on the catalog and product pages.

I have used the following PHP:

<?php // sale badge ?>
<?php
    $specialPrice = $_product->getSpecialPrice();
    $specialPriceFromDate = $_product->getSpecialFromDate();
    $specialPriceToDate   = $_product->getSpecialToDate();
?>
<?php if ($specialPrice || ($currentDate >= $specialPriceFromDate && $currentDate <= $specialPriceToDate) || ($currentDate >= $specialPriceFromDate && $specialPriceFromDate != '' && $specialPriceToDate == '')): ?>
    <span class="sale-badge"><?php echo $this->__('Sale') ?></span>
<?php endif; ?>

This displays the badge correctly unless the special price to date has passed when it still displays the badge even though the product is no longer at the special price.

The idea is for the badge to display in the following scenarios:

  1. there are no special from or special to dates set (i.e. the product is permanently on special until manually altered)
  2. there is a from date but no to date (i.e. once the from date passes the product is permanently on special until manually altered)
  3. there is no from date but there is a to date (i.e. the product is on special until the to date passes)
  4. there is a from and a to date (i.e. the product is only on special within the defined period)

I am sure I have just missed something obvious but can someone point out what it is please?

Was it helpful?

Solution

Hugh,You donot need lot of codes.Just check final price is less then product price.Final price is calculate automatically.

Try this

<?php if($_product->getFinalPrice()<$_product->getPrice()): ?>
  <span class="sale-badge"><?php echo $this->__('Sale') ?></span>
    <?php endif; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top