0

I have a table on my home page which shows 5 products from a category, I made called "Recommended For You". This works perfectly fine.

The only thing I want to add is if the product has a special price (is on sale), I want a little image to appear in the product image. I have done this in other places on my website by giving them the class "sale-label". However, I can't set this class to all the product images as not all of them are on sale.

I assume I need an if statement to check if the product has a special price. If true, then set the class to "sale-label".

However, I do not know how to code this if anyone could help.

This is my code which I use to show the products on my home page.

<div class="random_featured_products">
<?php
Mage::getSingleton('catalog/layer')->setData("product_collection",NULL);
$this->_productCollection = null;
$_productCollection=$this->getLoadedProductCollection();
$_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));

?>
    <h4 class="recommended_title">
    <!--class="Recommended_title"-->
    <?php echo $this->__("Recommended For You");?></h4>
    <hr class="title_left">
<div class="products-grid two_columns_5">
<?php $_productCollection=$this->getLoadedProductCollection() ?>
<?php if(!$_productCollection->count()): ?>
<div class="padder">
<div class="note-msg">
    <?php echo $this->__('There are no products matching the selection.') ?>
</div>
</div>
<?php else: ?>

<?php // Grid Mode ?>
 <!-- the class name will change to .listing-type-cell if viewing in list mode -->
<?php $_collectionSize = $_productCollection->count() ?>

<?php $_items = $_productCollection->getItems(); 
           shuffle($_items); ?>

    <table>
    <?php $i=0; foreach ($_items as $_product): ?>
    <?php if ($i++%4==0): ?>
    <tr>
    <?php endif ?>

    <ol class="grid ">
        <li class="item1">
            <p class="product-image">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getShortName()) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>" alt="<?php echo $this->htmlEscape($_product->getShortName()) ?>"/>
                </a>
            </p>
            <p  class="product_desc" ><a  href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getShortName()) ?>"><?php echo $this->htmlEscape($_product->getShortName()) ?></a></p>
            <?php if($_product->getRatingSummary()): ?>
                <!--?php echo $this->getReviewsSummaryHtml($_product, 'short') ?-->
                <?php echo $this->getReviewsSummaryHtml($_product) ?>
            <?php endif; ?>

            <div class="pro_price">
            <?php echo $this->getPriceHtml($_product, true) 
            ?>
            </div>

        </li>
    <?php if ($i%5==0 && $i!=$_collectionSize): ?>
    </tr>
    <?php endif ?>

    <?php if ($i==5) break;  // show 5 products max ?> 

    <?php endforeach ?>
    <?php for($i;$i%5!=0;$i++): ?>
          <td class="empty-product">&nbsp;</td>
    <?php endfor ?>
    <?php if ($i%5==0): ?>
    </tr>
    <?php endif ?>
    </table>
    <script type="text/javascript">decorateTable('product-list-table')</script>
</div>
</div>
<?php endif; ?>

Thanks for any help

4

Here is how you can see if a product has a special price:

if ($_product->getFinalPrice() < $_product->getPrice()) {
    $hasSpecialPrice = true;
}
else {
    $hasSpecialPrice = false;
}

or on one line:

$hasSpecialPrice = ($_product->getFinalPrice() < $_product->getPrice());

Now all you need to do is integrate this in the code you have and add the 'sale-label' class where you want.

Here is a clearer example on how you can add a class to an element based on the condition above.

<div class=<?php echo ($_product->getFinalPrice() < $_product->getPrice() ? 'sale-label' : '')?>
    <!-- div content here -->
</div>
  • then do i write another IF statement saying if ($hasSpecialPrice == true) { set class to sale-label} obviously not exactly like that though lol – Adam Allen Jun 30 '14 at 12:11
  • @AdamAllen. See my update to the answer. Maybe it helps now. – Marius Jun 30 '14 at 12:22

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.