【发布时间】:2011-07-09 22:51:16
【问题描述】:
我尝试了很多东西,但它们都不起作用。我想我可以在产品页面上获取自定义属性,但我想知道:如何在购物车页面中获取它们? (属性只是简单的书面文字)
【问题讨论】:
标签: php html magento custom-attributes
我尝试了很多东西,但它们都不起作用。我想我可以在产品页面上获取自定义属性,但我想知道:如何在购物车页面中获取它们? (属性只是简单的书面文字)
【问题讨论】:
标签: php html magento custom-attributes
$_item->getProduct()->load() 将重新加载数据库中的所有产品数据。虽然这会起作用,但请记住,每次调用 load() Magento 时都会执行数据库查询。
同样可以通过将属性与引用项一起加载来获得更好的性能。只需创建一个自定义模块并将其添加到 config.xml
<global>
<sales>
<quote>
<item>
<product_attributes>
<one_custom_attribute_code />
<another_custom_attribute_code />
</product_attributes>
</item>
</quote>
</sales>
</global>
完成后,您无需额外的数据库查询即可访问您的自定义属性。
$_item->getProduct()->getAnotherCustomAttributeCode();
这是一篇关于此的文章:https://www.atwix.com/magento/accessing-custom-attribute-at-checkout-or-cart/
【讨论】:
$_item->getProduct()->getResource()->getAttribute('attribute_code')->getStoreLabel();获取标签。
$_item->getProduct()->getAttributeText('attribute_code') 应该可以解决问题。
你说的是自定义选项还是简单属性?
简单属性(文字):
(在你的 default.phtml 中)
<?php $_item = $this->getItem()?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
【讨论】:
我用过这个
(在app/design/frontend/default/your_theme/template/checkout/cart/item/default.phtml)
对于我的(文本字段)属性:
<?php
$_item = $this->getItem();
$_product = $_item->getProduct()->load();
?>
<?php echo $_product->getCustomAttribute(); ?>
【讨论】:
这不是最好的方法,在你的属性(magento/admin)中你可以设置选项:
在结帐中可见
所以属性转到了
$_options Array ($_options = $this->getOptionList()) (in checkout/cart/item/default.phtml)
您可以像这样使用属性(数组$_option):
array(4) { ["label"]=> string(10) "Lieferzeit" ["value"]=> string(8) "2-3 Tage" ["print_value"]=> string(8) "2-3 Tage" ["code"]=> string(13) "delivery_time" }
通过这种方式,您无需再次连接数据库并优化性能。
【讨论】:
$_item->getProduct()->load(); 好得多。 IMO 每次使用 load 时都应该重新考虑他们在做什么 - 负载可能非常激烈,实际上会降低整体渲染性能。然而,最好的方法是上面 Andreas Riedmüller 指出的方法。
显示从选项列表中选择的属性:
更改:app/design/frontend/base/default/template/checkout/cart/item/default.phtml
$_customOptions = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
foreach($_customOptions['attributes_info'] as $_option){ echo option['label']; }
【讨论】:
一种可能的方法是使用 singleton 设计模式。这是获取属性的方法。
$_item = $this->getItem();
$_product= Mage::getSingleton('catalog/product')->load($_item->getProductId());
echo $attrValue=$_product->getAttributeText('attrCode');
【讨论】:
在所有的贡献之后,我得到了第一个答案并想知道围绕 magento。
我找到了一个无需再次进行 load() 的解决方案。我已经在以下路径上编辑了文件 config.xml,
app/code/core/Mage/Sales/etc/config.xml
在商品/产品属性上我添加了自定义属性
<item>
<product attributes>
<sku/>
<type_id/>
<my_custom_attribute_id/>
然后在我的 cart.phtml 文件中,我可以通过以下方式获取属性:
$_item->getProduct()->getmy_custom_attribute_id();
我不知道这是最好的还是正确的做法,但它确实解决了问题。
干杯
【讨论】:
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
【讨论】: