如果没有退回产品,销售产品不显示标题。

时间:2014-05-22 12:13:48

标签: php magento

我有这个代码显示4个正在出售的产品,但我想要包装在IF声明基本上说如果没有产品然后不显示任何东西。例如,没有产品它仍然显示标题"这些伟大的产品正在销售"。我知道这似乎是一个简单的任务,但是当你还没有真正使用PHP时,它会非常紧张。

<?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>

谢谢你能提供帮助。

1 个答案:

答案 0 :(得分:0)

如果您不想显示标题,如果没有从集合中返回产品,那么您可以执行以下操作

<?php if($_productCollection->count() > 0):?>
<h1 class="sale_title"> These Great Products Are On Sale</h1>
<?php endif;?>

您可以在<div>条件下包装所需的IF