【问题标题】:Magento 2: How to get multiple selection attibute as list on list.phtmlMagento 2:如何在 list.phtml 上获取多个选定属性作为列表
【发布时间】:2017-11-21 02:13:52
【问题描述】:

我已经为我的产品添加了一个多选属性,现在我正试图在“list.phtml”模板上显示结果。 (产品列表)。

我得到了这样的所有值:

$attribute = $_product->getResource()->getAttribute('attribute_name')->getFrontend()->getValue($_product);

这会返回一个包含所有值的字符串,但我需要它是一个数组,这样我就可以将它转换为带有单个链接的列表。

有什么帮助吗?

谢谢!

【问题讨论】:

    标签: magento2


    【解决方案1】:

    最终使用:

    $attribute_string = $_product->getResource()->getAttribute('attribute')->getFrontend()->getValue($_product);
    
    $attribute_array = explode(', ', $attribute_string);
    

    【讨论】:

    • 如何以编程方式设置值?
    • 但是如果值中已经有逗号怎么办?在这种情况下,它将单个值拆分为两个不正确的数组项。
    【解决方案2】:

    将对象转换为数组。

    $data = 数组($attribute);

    或者

    $data = (array) $attribute;

    【讨论】:

    • 感谢 Jitendra 我最终使用了 $data = explode(', ', $string);
    猜你喜欢
    • 2011-04-28
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多