2

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?

2

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; ?>
  • Thank you - I was worried that this approach would include items that are part of the fixed price bundles I have as these have a lower price than the items contained in them but it doesn't so accepted as answered. – Hugh Feb 10 '15 at 7:14

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.