Magento-使用config.xml在购物车中检索自定义属性标签

Magento-使用config.xml在购物车中检索自定义属性标签,magento,attributes,cart,Magento,Attributes,Cart,我需要在购物车中显示一个自定义属性。该属性是一个下拉列表,如下所示: 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 要显示这一点,我有一个自定义模块,其中包含我的conf

我需要在购物车中显示一个自定义属性。该属性是一个下拉列表,如下所示:

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>
这将返回属性(即67)的ID/值,但我希望能够获取标签(即仓库)

我知道我可以从id中获取标签,如下所示:

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

但是我想通过我的模块获得标签,这样我就可以省去额外的数据库查询,并且不得不再次加载产品模型。我的购物车每个订单可以有20多个商品,因此使用最后一种方法可以稍微降低速度。

您可以使用
$\u item->getProduct()->getAttributeText('section')
,这更清楚,但我没有研究性能。我认为,如果您在已包含属性的集合中加载产品,则不需要对每个产品/属性单独调用数据库。

我假设您可以访问Mage\u Sales\u Model\u Quote\u Item对象。如果是,您可以尝试以下代码段:

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

完全正确性能似乎也没有受到影响。谢谢如果您想获得StoreLabel而不是AttributeText,您会怎么做?当页面点击我的代码时,如果页面没有死掉,我似乎无法获得StoreLabel<代码>$\u item->getProduct->getAttribute('attribute\u code')->getStoreLabel()
$_resource = $_item->getProduct()->getResource();
$optionValue = $_resource
    ->getAttributeRawValue($_item->getProduct()->getId(), 'section', Mage::app()->getStore()->getId());
$optionLabel = $_resource
    ->getAttribute('section')
    ->getSource()
    ->getOptionText($optionValue);