0

I have this code which displays 4 products which are on sale, however i want want wrap it in an IF statement basically saying IF there are no products then dont display anything. For example fi there are no products it still shows up the title "These Great Products Are On Sale". I know this seems like a simple task but when you havent really worked with PHP then it is very stressful.

<?php
Mage::getSingleton('core/session', array('name' => 'frontend'));
$_category = Mage::registry('current_category');
            $currentCategoryId= $_category->getId();

$_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addCategoryFilter($_category)
            ->addStoreFilter();

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($_productCollection);


$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);

$_productCollection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
    ->addAttributeToFilter('special_to_date', array('or'=> array(
    0 => array('date' => true, 'from' => $tomorrowDate),
    1 => array('is' => new Zend_Db_Expr('null')))
    ), 'left');

?>

<div class="for_sale_div">
<!--heading for sale products-->

<h1 class="sale_title"> These Great Products Are On Sale</h1>

<!-- set it into a grid format -->
<div class="products-grid two_columns_5">

<?php $_collectionSize = $_productCollection->count() ?>
<?php $i=0; foreach($_productCollection as $_product):?>
<?php if($i++%3==0): ?>

<ol class="grid">
 <?php endif; ?>

<li class="item">


<p class="product-image">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>"
                        width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>"
                        title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
                    </a>
                </p>
                <h5><a href="<?php echo $_product->getProductUrl() ?>" 
                title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                <?php echo $this->htmlEscape($_product->getName()) ?></a></h5>

                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>

                <?php echo $this->getPriceHtml($_product, true) ?>
                 </li>

        <?php if($i%3==0 || $i==$_collectionSize): ?>
        </ol>
        <?php endif; ?>
    <!-- Set the product count to 4-->
    <?php if($this->getIsHomepage() && $i==4) break; ?>

 <?php endforeach ?>
 </div>
 </div>

Thank you if you can help.

1

Yu can use $_productCollection->getSize() to get the number of products in the collection.

For example


if ($_productCollection->getSize() > 0) {
   \\do something
}

Be aware that getSize() does not load the collection. If you need the loaded collection anyway, you can also use count($_productCollection), which loads the collection.

  • Also i dont know if you will be able to help with this, but i am getting the category ID of the current category, the only problem with this is i can only display the sales on the product landing page, but i want it to be able to be put on the main category page so it can search the products in that categories sub categories and display them. Hope this makes sense if not let me know and will give an example. – Adam Allen May 22 '14 at 13:44
  • @AdamAllen it should work the same way. You get the products of the child categories if you set the is_anchor flag for the top category. – Thorsten May 23 '14 at 6:49
  • I have set the parent category to an anchor so it now says there are products in the cat (from the sub cats) but it not showing any products when i add my static block – Adam Allen May 23 '14 at 9:06

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.