【问题标题】:Magento shows attribute if value is left blank如果值留空,Magento 显示属性
【发布时间】:2013-06-26 11:53:09
【问题描述】:

我有一个用于warranty_labour 的下拉magento 属性。我正在使用我的主题 product/view.phtml 提取属性,然后附加一个图标以显示您获得的保修。

我已经使用了成功运行的代码:

<?php $warranty=$_product->getAttributeText('warranty_labour'); echo '<img style="padding-top: 10px;" src="/images/warrantylabour/'.str_replace(' ', '_',$warranty).'.png" alt="'.$warranty.'">'; ?>

当产品没有设置保修属性时我发现的问题(后端留空)我仍然将代码插入到前端的产品源代码中,如下所示:

<img style="pad`ding-top: 10px;" src="/images/warrantylabour/.png" alt="">`

当属性中没有设置值时,有没有办法阻止这种情况发生?

【问题讨论】:

    标签: image magento attributes icons


    【解决方案1】:

    不要忘记逻辑! :-) 只需使用典型的 Magento 模板约定测试 $warranty 是否有值:

    <?php if($warranty=$_product->getAttributeText('warranty_labour')): ?>
        <?php echo sprintf('<img style="padding-top: 10px;" src="/images/warrantylabour/%s.png" alt="%s"/>', str_replace(' ', '_',$warranty),$warranty) ?>
    <?php endif; ?>
    

    我认为,这种语法可能证明将此逻辑和字符串构建封装到辅助方法中是合理的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多