【问题标题】:How to show the message “No Products” when there's no products in my custom block?当我的自定义块中没有产品时,如何显示“没有产品”消息?
【发布时间】:2016-04-02 00:09:52
【问题描述】:

当我的块中没有可显示的内容时,我需要显示消息“没有产品”或“没有与选择匹配的产品”。

    <?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 来显示产品价格。

我试过&lt;?php echo $this-&gt;getPriceHtml($_item, true) ?&gt;

【问题讨论】:

    标签: html static block frontend magento-1.9


    【解决方案1】:

    你去: (建议:使用 &lt;?php if(): ?&gt; 等封闭标签以获得更好的可读性)

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

    【讨论】:

    • 我已经像 jsfiddle.net/fhp3uvbu 这样添加了您的代码,但是即使属性“getMerchantName”中的产品很少,我也没有收到任何产品消息
    • @user6036056 你实际上不能在这个 jsfiddle 上运行你的 PHP .. 它的设计目的不是以这种方式解释 PHP。最好的办法是开始调试。将 echo "functionName started"print_r(array()) 当 Object 或 Array 放在脚本中被调用函数的每个开头和结尾(返回、中断或异常之前)。检查错误跟踪将带你多深,你应该知道是什么导致它不起作用。
    猜你喜欢
    • 2018-09-08
    • 2017-09-19
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2015-01-09
    • 2020-10-12
    相关资源
    最近更新 更多