pomaaa
pomaaa

Reputation: 615

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)

Upvotes: 16

Views: 36423

Answers (8)

arvinda kumar
arvinda kumar

Reputation: 709

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

Upvotes: 0

Tiago H Moreira
Tiago H Moreira

Reputation: 21

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

Upvotes: 0

Sander Looijenga
Sander Looijenga

Reputation: 129

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

Upvotes: 6

kileMANjaro
kileMANjaro

Reputation: 9

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

Upvotes: 1

Marco
Marco

Reputation: 11

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.

Upvotes: 1

Andreas Riedm&#252;ller
Andreas Riedm&#252;ller

Reputation: 1439

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

Upvotes: 51

user1668260
user1668260

Reputation: 27

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

Upvotes: 0

Antonino Bonumore
Antonino Bonumore

Reputation: 797

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

Upvotes: 25

Related Questions