【问题标题】:Magento: How do I separate the display of multiple attributes?Magento:如何分离多个属性的显示?
【发布时间】:2012-05-10 20:30:37
【问题描述】:

我正在使用此代码在 Magento 中显示(在前端)产品的所有分配属性:

<?php if ($_product->getData('metal')): ?>  
    <?php echo nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product )) ?>  
<?php endif; ?>

我需要用项目符号&amp;#149; 和休息&lt;br/&gt; 分隔每个项目。我将如何编辑此代码以反映这些更改? 提前致谢。

_山姆

【问题讨论】:

  • 使用jQuery来操作客户端。
  • 你能更具体一点吗?我将在哪里/插入什么代码? Teşekkürler!
  • Sam,你有直播网站吗?给我我会尝试用 jQuery 操作的链接。另外,我会查一下它的来源。
  • Oguz,我没有实时站点。但这是基于该代码当前显示的内容(例如):“18k 金,纯银”。如果我能确定它是从哪里得到逗号的,我觉得我可以很容易地把它换成休息和子弹
  • 我找到了,但我不确定您正在寻找的功能。转到/app/code/core/Mage/Eav/Model/Entity/Attribute/Frontend/Abstract.php 并找到getValue 函数。您将看到一个内爆函数,它正在内爆属性值。尝试并分享结果。

标签: php magento content-management-system e-commerce


【解决方案1】:

如果您想对输出进行更多控制(也许您想设置单个值的样式),请使用 explode();

<?php $attr = explode("," ,  $_helper->productAttribute($_product, $_data['value'], $_data['code'])); 
echo "<ul>";
?>
<?php foreach ($attr as $attrValue) {
    echo "<li>" . $attrValue . "</li>";
} 
echo "</ul>";
?>

【讨论】:

    【解决方案2】:

    假设上面的代码是“工作的”...做这个改变:

    <?php echo "&#149;".nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product ))."<br/>" ?>
    

    . 是一个字符串连接运算符。

    或者如果上面是产品列表的 HTML,您是在问怎么做?那么这应该工作......

    <?php 
    $somehtml=nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product ));
    $somehtml=explode("<br />",$somehtml);  // This ASSumes nl2br inserts <br /> between each line and this 
                                            // would lead to the behaviour you want.
                                            // see: http://php.net/manual/en/function.nl2br.php
    $somehtml=implode("<br/>\n&#149;",$somehtml);
    $somehtml="&#149;".$somehtml."<br/>\n";
    echo $somehtml;
    unset($somehtml);
    ?>
    

    explode 通过"&lt;br /&gt;" 切分字符串,从字符串创建一个数组。 implode 然后通过在每个元素之间放置 &lt;br/&gt;\n&amp;#149; 重新加入字符串。最后,我将项目符号点添加到前面并在末尾添加 br。内爆时,您仍然必须考虑字符串的“外部”。

    【讨论】:

    • 谢谢克里斯。我发布的原始代码显示了所有属性,但它们用逗号分隔,而不是项目符号或中断。我尝试了你们的两段代码,我得到了相同的结果,用逗号分隔并在同一行。
    • 如果我能弄清楚逗号是从哪里来的;看来我可以用break和bullet替换它。
    • 嗯,也许尝试将第一个爆炸更改为$somehtml=explode(",",$somehtml); 然后它会在那个逗号上工作??
    【解决方案3】:

    解决了。 很简单:如果您的默认包中没有,请从 base 复制 attributes.phtml。

    放入app/design/frontend/default/default/template/catalog/product/view/文件夹

    将第 46 行更改为:

    <td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
    

    显示单独的产品属性:

    <td class="data"><?php echo $_helper->productAttribute($_product, str_replace(",", "<br />", $_data['value']), $_data['code']) ?></td> 
    

    【讨论】:

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