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

发布于 2024-10-20 17:08:02 字数 76 浏览 4 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

倒带 2024-10-27 17:08:02

$_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/访问自定义属性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 call load() 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

<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/

失而复得 2024-10-27 17:08:02

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

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

Are you talking about custom option or simple attribute?

Simple attribute (text):
(In your 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); ?>
缺⑴份安定 2024-10-27 17:08:02

我使用这个

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

作为我的 (textfield) 属性:

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

I used this

(in 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(); ?>
萤火眠眠 2024-10-27 17:08:02

这不是最好的方法,在您的属性(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" } 

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

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

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:

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.

ゞ花落谁相伴 2024-10-27 17:08:02

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

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

Show selected attribute from option list:

Change in: 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']; }
人事已非 2024-10-27 17:08:02

一种可能的方法是使用单一设计模式。下面介绍如何获取属性。

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

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

$_item = $this->getItem();
$_product= Mage::getSingleton('catalog/product')->load($_item->getProductId());
echo $attrValue=$_product->getAttributeText('attrCode');
过气美图社 2024-10-27 17:08:02

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

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

干杯

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,

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:

$_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

哀由 2024-10-27 17:08:02
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
                <?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
                <?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文