php 如何在购物车中显示自定义属性(Magento)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5213229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:39:22  来源:igfitidea点击:

How to display custom attribute in cart (Magento)

phphtmlmagentocustom-attributes

提问by pomaaa

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)

我尝试了很多东西,但没有一个起作用。我想我可以在产品页面上获取自定义属性,但我想知道:如何在购物车页面中获取它们?(属性只是一个简单的书面文字)

回答by Andreas Riedmüller

$_item->getProduct()->load()will reload all product data from the database. While this will work, bear in mind that every time you call load()Magento will execute a database query.

$_item->getProduct()->load()将从数据库重新加载所有产品数据。虽然这行得通,但请记住,每次调用load()Magento 时都会执行一次数据库查询。

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

通过将属性与引用项一起加载,可以以更好的性能完成相同的操作。只需创建一个自定义模块并将其添加到 config.xml

<global>
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <one_custom_attribute_code />
                    <another_custom_attribute_code />
                </product_attributes>
            </item>
        </quote>
    </sales>
</global>

Having done that, you can access your custom attribute without additional database queries.

完成后,您无需额外的数据库查询即可访问您的自定义属性。

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

Here is an article about this: https://www.atwix.com/magento/accessing-custom-attribute-at-checkout-or-cart/

这是一篇关于此的文章:https: //www.atwix.com/magento/accessing-custom-attribute-at-checkout-or-cart/

回答by Antonino Bonumore

Are you talking about custom option or simple attribute?

您是在谈论自定义选项还是简单属性?

Simple attribute (text):
(In your default.phtml)

简单属性(文本):(
在您的 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); ?>

回答by Sander Looijenga

I used this

我用过这个

(in app/design/frontend/default/your_theme/template/checkout/cart/item/default.phtml)

(在app/design/frontend/default/your_theme/template/checkout/cart/item/default.phtml)

for my (textfield) attribute:

对于我的(文本字段)属性:

<?php 
$_item = $this->getItem();
$_product = $_item->getProduct()->load();
?>
<?php echo $_product->getCustomAttribute(); ?>

回答by Marco

Thats not the best way, in your Attribute (magento/admin) you can set the option:

那不是最好的方法,在您的属性(magento/admin)中,您可以设置选项:

Visible in Checkout

在结帐中可见

So the Attribute goes to the

所以属性去

$_options Array ($_options = $this->getOptionList()) (in checkout/cart/item/default.phtml)

You can use the Attribute (the array $_option) like:

您可以使用属性(数组$_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" } 

In this way you wont need to connect the database again and you optimize the performance.

这样您就不需要再次连接数据库并优化性能。

回答by kileMANjaro

Show selected attribute from option list:

从选项列表中显示选定的属性

Change in: app/design/frontend/base/default/template/checkout/cart/item/default.phtml

更改在: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']; }

回答by Tiago H Moreira

Following all the contributions I've taken the 1st answer and wondered arround magento.

在所有的贡献之后,我接受了第一个答案,并想知道在 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,

我找到了一个无需再次执行 load() 的解决方案。我在以下路径上编辑了文件 config.xml,

app/code/core/Mage/Sales/etc/config.xml

and on the item / product attributes I've added the custom attribute

在项目/产品属性上,我添加了自定义属性

<item>
    <product attributes>
        <sku/>
        <type_id/>
        <my_custom_attribute_id/>

then on my cart.phtml file I was able to get the attribute just by using:

然后在我的 cart.phtml 文件中,我可以使用以下命令获取该属性:

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

I don't know if this is the best or the correct thing to do, but it sure solved the problem.

我不知道这是否是最好的或正确的做法,但它确实解决了问题。

Cheers

干杯

回答by arvinda kumar

<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
                <?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>

回答by user1668260

One possible method is to use a singletondesign pattern. Here is how to get an attribute.

一种可能的方法是使用单例设计模式。这是获取属性的方法。

$_item = $this->getItem();
$_product= Mage::getSingleton('catalog/product')->load($_item->getProductId());
echo $attrValue=$_product->getAttributeText('attrCode');