Magento - 使用 config.xml 在购物车中检索自定义属性标签
Magento - Retrieve custom attribute LABEL in cart with config.xml
我有一个自定义属性需要在购物车中显示。该属性是一个下拉列表,如下所示:
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();
这个returns属性的ID/VALUE(即67)但是我希望能够得到LABEL(即仓库)
我知道我可以像这样从 id 中获取标签:
$_product = Mage::getModel('catalog/product');
$attr = $_product->getResource()->getAttribute("section");
if ($attr->usesSource()) {
$_label = $attr->getSource()->getOptionText("67");
}
但我想通过我的模块获取标签,这样我就可以删除额外的数据库查询,而不必再次加载产品模型。我的购物车每个订单可以包含超过 20 件商品,因此使用最后一种方法可以稍微减慢它的速度。
你可以用$_item->getProduct()->getAttributeText('section')
代替,这样更清楚,但我没有研究性能。我认为,如果您将产品加载到已经包含该属性的集合中,那么它不需要每个 product/attribute.
单独的数据库调用
我假设您可以访问 Mage_Sales_Model_Quote_Item 对象。如果是这样,您可以尝试以下代码段:
$_resource = $_item->getProduct()->getResource();
$optionValue = $_resource
->getAttributeRawValue($_item->getProduct()->getId(), 'section', Mage::app()->getStore()->getId());
$optionLabel = $_resource
->getAttribute('section')
->getSource()
->getOptionText($optionValue);
我有一个自定义属性需要在购物车中显示。该属性是一个下拉列表,如下所示:
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();
这个returns属性的ID/VALUE(即67)但是我希望能够得到LABEL(即仓库)
我知道我可以像这样从 id 中获取标签:
$_product = Mage::getModel('catalog/product');
$attr = $_product->getResource()->getAttribute("section");
if ($attr->usesSource()) {
$_label = $attr->getSource()->getOptionText("67");
}
但我想通过我的模块获取标签,这样我就可以删除额外的数据库查询,而不必再次加载产品模型。我的购物车每个订单可以包含超过 20 件商品,因此使用最后一种方法可以稍微减慢它的速度。
你可以用$_item->getProduct()->getAttributeText('section')
代替,这样更清楚,但我没有研究性能。我认为,如果您将产品加载到已经包含该属性的集合中,那么它不需要每个 product/attribute.
我假设您可以访问 Mage_Sales_Model_Quote_Item 对象。如果是这样,您可以尝试以下代码段:
$_resource = $_item->getProduct()->getResource();
$optionValue = $_resource
->getAttributeRawValue($_item->getProduct()->getId(), 'section', Mage::app()->getStore()->getId());
$optionLabel = $_resource
->getAttribute('section')
->getSource()
->getOptionText($optionValue);