如何在购物车中显示自定义属性(Magento)
我尝试了很多东西,但没有一个起作用。我想我可以在产品页面上获取自定义属性,但我想知道:如何在购物车页面中获取它们? (属性只是简单的文字)
I have tried a lot of stuf but none of them work. I think I can get the custom attributes on the product page, but I was wondering: how to get them in shopping cart page? (the attribute is just a simple written text)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
$_item->getProduct()->load()
将从数据库重新加载所有产品数据。虽然这可行,但请记住,每次调用load()
时,Magento 都会执行数据库查询。通过将属性与报价项一起加载,可以实现相同的效果,并获得更好的性能。只需创建一个自定义模块并将其添加到 config.xml 中即可
。完成此操作后,您无需额外的数据库查询即可访问自定义属性。
这是一篇关于此的文章: https://www.atwix.com/ magento/访问自定义属性at-checkout-or-cart/
$_item->getProduct()->load()
will reload all product data from the database. While this will work, bear in mind that every time you callload()
Magento will execute a database query.The same can be done with much better performance by loading the attribute along with the quote item. Just create a custom module and add this to the config.xml
Having done that, you can access your custom attribute without additional database queries.
Here is an article about this: https://www.atwix.com/magento/accessing-custom-attribute-at-checkout-or-cart/
您是在谈论自定义选项还是简单属性?
简单属性(文本):
(在您的default.phtml中)
Are you talking about custom option or simple attribute?
Simple attribute (text):
(In your default.phtml)
我使用这个
(在
app/design/frontend/default/your_theme/template/checkout/cart/item/default.phtml
中)作为我的 (textfield) 属性:
I used this
(in
app/design/frontend/default/your_theme/template/checkout/cart/item/default.phtml
)for my (textfield) attribute:
这不是最好的方法,在您的属性(magento/admin)中,您可以设置选项:
在结帐中可见,
因此该属性将转到
您可以使用该属性(数组
$_option
),例如:这样您就不需要再次连接数据库并优化性能。
Thats not the best way, in your Attribute (magento/admin) you can set the option:
Visible in Checkout
So the Attribute goes to the
You can use the Attribute (the array
$_option
) like:In this way you wont need to connect the database again and you optimize the performance.
显示从选项列表中选择的属性:
更改:app/design/frontend/base/default/template/checkout/cart/item/default.phtml
Show selected attribute from option list:
Change in: app/design/frontend/base/default/template/checkout/cart/item/default.phtml
一种可能的方法是使用单一设计模式。下面介绍如何获取属性。
One possible method is to use a singleton design pattern. Here is how to get an attribute.
在所有贡献之后,我接受了第一个答案,并想知道 magento 周围的情况。
我找到了一个解决方案,我不必再次进行 load() 。我已经在以下路径上编辑了文件 config.xml,
并且在项目/产品属性上添加了自定义属性,
然后在 cart.phtml 文件上我可以通过使用以下方式获取该属性:
我不知道这是否是最好的或正确的做法,但它确实解决了问题。
干杯
Following all the contributions I've taken the 1st answer and wondered arround magento.
I found a solution that I didn't have to make the load() again. I've edited the file config.xml on the following path,
and on the item / product attributes I've added the custom attribute
then on my cart.phtml file I was able to get the attribute just by using:
I don't know if this is the best or the correct thing to do, but it sure solved the problem.
Cheers