简体   繁体   English

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

[英]How to display custom attribute in cart (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) (属性只是一个简单的书面文字)

$_item->getProduct()->load() will reload all product data from the database. $_item->getProduct()->load()将从数据库中重新加载所有产品数据。 While this will work, bear in mind that every time you call load() Magento will execute a database query.虽然这会起作用,但请记住,每次调用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/

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); ?>

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(); ?>

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.这样您就不需要再次连接数据库并优化性能。

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

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');

You can do the following to display custom product attribute, in this case warranty product attribute on product view page. 您可以执行以下操作来显示自定义产品属性,在本例中为产品视图页面上的保修产品属性。

Step 1: Create a new attribute 'warranty' in Magento back end under STORES->Attributes->Product . 步骤1:STORES->Attributes->Product下的Magento后端创建一个新属性'warranty'。 Set the values to “YES” in “Visible on Product View Page on Front-end” and “Used in Product Listing” options under storefront Properties while creating the attribute to display it in Product list and view pages. 在创建属性以在“产品列表”和“查看”页面中显示属性时,将“在前端的产品视图页面上可见”和“在产品列表中使用”选项中的值设置为“是”,然后创建属性。

Step 2: Assign the new attribute 'warranty' to the default attribute set in STORES->Attributes->Attribute set. 步骤2:将新属性'warranty'分配给STORES->Attributes->Attribute set中的默认STORES->Attributes->Attribute集。

Step 3: Now you will see the attribute while creating the products. 第3步:现在您将在创建产品时看到该属性。 You can give whatever value you want. 你可以提供你想要的任何价值。

Step 4: Edit the catalog_product_view.xml file and update the following content. 步骤4:编辑catalog_product_view.xml文件并更新以下内容。 You can add this section inside the product.info.main container. 您可以在product.info.main容器中添加此部分。 You can add your block next to product.info.overview block. 您可以在product.info.overview块旁边添加块。 So it will be displayed next to the short description. 因此它将显示在简短描述旁边。

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.demo" template="product/view/demo.phtml" after="-">
<arguments>

<argument name="at_call" xsi:type="string">getWarranty</argument>

<argument name="at_code" xsi:type="string">warranty</argument>

<argument name="css_class" xsi:type="string">warranty </argument>

<argument name="at_label" xsi:type="string">warranty </argument>

<argument name="add_attribute" xsi:type="string">itemprop="warranty "</argument>

</arguments>

</block>

Step 5: Create a new file warranty.phtml under mageno2root/vendor/magento/module-catalog/view/frontend/templates/product/view with the below content. 步骤5:使用以下内容在mageno2root / vendor / magento / module-catalog / view / frontend / templates / product / view下创建一个新文件warranty.phtml

<?php

$_helper = $this->helper('Magento\Catalog\Helper\Output');

$_product = $block->getProduct();

$_code = $block->getAtCode();

$_className = $block->getCssClass();

$_attributeLabel = $block->getAtLabel();

$_attributeType = $block->getAtType();

$_attributeAddAttribute = $block->getAddAttribute();



if ($_attributeLabel && $_attributeLabel == 'default') {

$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();

}

$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);

?>

<?php if ($_attributeValue): ?>

<div class="product attibute <?php echo $_className?>">

<?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?>

<div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>

</div>

<?php endif; ?>

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.我找到了一个无需再次执行 load() 的解决方案。 I've edited the file config.xml on the following path,我在以下路径上编辑了文件 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干杯

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM