【问题标题】:Magento - Retrieve custom attribute LABEL in cart with config.xmlMagento - 使用 config.xml 在购物车中检索自定义属性 LABEL
【发布时间】:2015-03-31 14:32:13
【问题描述】:

我有一个需要在购物车中显示的自定义属性。该属性是一个像这样的下拉列表:

Attribute Code : section
Catalog Input Type for Store Owner : Dropdown
Options:
    ID/VALUE = 67 LABEL = warehouse
    ID/VALUE = 69 LABEL = showroom
    ID/VALUE = 70 LABEL = stockroom

为了显示这个,我有一个自定义模块和我的 config.xml,如下所示:

<global>
<sales>
    <quote>
        <item>
            <product_attributes>
                <section/>
            </product_attributes>
        </item>
    </quote>
</sales>

我可以在购物车中调用:

$_item->getProduct()->getSection();

这将返回属性的 ID/VALUE(即 67),但我希望能够获取 LABEL(即仓库)

我知道我可以像这样从 id 中获取标签:

$_product = Mage::getModel('catalog/product');
$attr = $_product->getResource()->getAttribute("section");
if ($attr->usesSource()) {
    $_label = $attr->getSource()->getOptionText("67");
}

但我想通过我的模块获取标签,这样我就可以删除额外的数据库查询并不得不再次加载产品模型。我的购物车每个订单可以有超过 20 件以上的商品,因此使用最后一种方法可以稍微减慢速度。

【问题讨论】:

    标签: magento attributes cart


    【解决方案1】:

    你可以改用$_item-&gt;getProduct()-&gt;getAttributeText('section'),这样更清楚,但我没有研究过性能。我认为,如果您将产品加载到已经包含该属性的集合中,那么每个产品/属性将不需要单独的数据库调用。

    【讨论】:

    • 正确。性能似乎也没有受到影响。谢谢
    • 如果您想要获取 StoreLabel 而不是 AttributeText,您会怎么做?我似乎无法获得 StoreLabel 没有页面在遇到我的代码时死亡。 $_item-&gt;getProduct-&gt;getAttribute('attribute_code')-&gt;getStoreLabel();
    【解决方案2】:

    我假设您有权访问 Mage_Sales_Model_Quote_Item 对象。如果是这样,你可以试试下面的sn-p:

    $_resource = $_item->getProduct()->getResource();
    $optionValue = $_resource
        ->getAttributeRawValue($_item->getProduct()->getId(), 'section', Mage::app()->getStore()->getId());
    $optionLabel = $_resource
        ->getAttribute('section')
        ->getSource()
        ->getOptionText($optionValue);
    

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 1970-01-01
      • 2011-07-09
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多