问题
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)
回答1:
$_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/
回答2:
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); ?>
回答3:
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(); ?>
回答4:
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.
回答5:
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']; }
回答6:
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');
回答7:
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
.
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.
Step 3: Now you will see the attribute while creating the products. You can give whatever value you want.
Step 4: Edit the catalog_product_view.xml
file and update the following content. You can add this section inside the product.info.main
container. You can add your block next to product.info.overview
block. 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.
<?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; ?>
回答8:
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
回答9:
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
来源:https://stackoverflow.com/questions/5213229/how-to-display-custom-attribute-in-cart-magento