如果我的自定义区块中没有产品,如何显示“无产品”消息?

时间:2016-04-02 00:09:52

标签: html static block frontend magento-1.9

我需要显示消息" No Products"或"没有符合选择的产品"当我的街区里没有任何东西可以显示时。

    <?php        
$manufacturer = Mage::registry('current_product')->getMerchantName();

$productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('merchant_name',$manufacturer);
$productCollection->getSelect()->order('RAND()');
$productCollection->getSelect()->limit(5);

foreach ($productCollection as $_product)
?>

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

<div class="product-details">
<p class="product-name"><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></p>

<?php   
$merchant_name = $_product->getAttributeText('merchant_name');
if ($merchant_name){?>
<div>by <?php echo $merchant_name;?> </div>
<?php }

else if ($_product->getIsEbayaffiliate()) { ?>
<div>by eBay</div>
<?php }

else { ?>
<div>by Home Done</div>
<?php } ?>

此外,我需要将getPriceHtml添加到上面的代码中以显示产品价格。

我试过了<?php echo $this->getPriceHtml($_item, true) ?>

1 个答案:

答案 0 :(得分:0)

你去了: (建议:使用<?php if(): ?>等封闭式标签以提高可读性)

<?php if(is_array($productCollection) && count($productCollection) ): ?>
    <?php foreach ($productCollection as $_product): ?>
        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(228) ?>" width="228" height="228" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
        <div class="product-details">
            <p class="product-name"><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></p>
        <?php $merchant_name = $_product->getAttributeText('merchant_name'); ?>
        <?php if ($merchant_name):?>
            <div>by <?php echo $merchant_name;?> </div>
        <?php elseif($_product->getIsEbayaffiliate()): ?>
            <div>by eBay</div>
        <?php else: ?>
            <div>by Home Done</div>
        <?php endif; ?>
        </div>
    <?php endforeach; ?>
<?php else: ?>
// here goes whatever you want to display if no products found in list  
<?php endif; ?>