Magento 1.9 – How to Access Product Custom Attribute in Cart Page and Order

cartcustom-attributesmagento-1.9ordersproduct

I have two product custom attributes Package and Tint.
I have assigned values to those attributes in my observer before add to cart in following way

   $item = $observer->getQuoteItem();
    // Ensure we have the parent item, if it has one
    $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

    // Set the custom price
    $item->setCustomPrice($custom_price);

    $item->setOriginalCustomPrice($custom_price);

    $item->setPackage('Silver'); // these values are depends upon customers selection
    $item->setTint('Green');

    // Enable super mode on the product.

    $item->getProduct()->setIsSuperMode(true);

then I tried to get that values in my cart.phtml page using

   $item->getPackage(); it returns nothing

What is correct method to access attribute values in cart page. Also I want to show those attributes in order for admin and customer

Best Answer

The quote items are kept in the db.
Since there are not columns to support your package and tint attributes the values you set to the item are not persistent.

So setting

$item->setPackage('Silver'); // these values are depends upon customers selection
$item->setTint('Green');

Makes these values available only in the current request.
When the page is realoaded, the quote items are retrieved from the db and they don't have package and tint.

You should add 2 more columns in the table sales_flat_quote_item that will hold these values.
Clear the cache when your are done.
To be able to access these values from the order items you need to do the same for the table sales_flat_order_item.
Add 2 columns and then tell magento that when it creates an order item from a quote item it should copy these fields also.

You can do this via the fieldsets from config.xml.

In one of your custom modules, inside the config.xml add this inside the <global> tag.

<fieldsets>
    <sales_convert_quote_item><!-- when converting a quote item -->
        <package> <!-- copy the package field ...-->
             <to_order_item>*</to_order_item> <!-- .. to the order item -->
        </package>
        <tint>
             <to_order_item>*</to_order_item>
        </tint>
    </sales_convert_quote_item>
</fieldsets>

Additional...if you want these values to be available on the invoice items, shipment items and creditmemo items, you need to add the same 2 columns in the tables sales_flat_invoice_item, sales_flat_shipment_item and sales_flat_creditmemo_item.

And inside the same <fieldsets> xml node as above add this:

<sales_convert_order_item>
    <package>
        <to_quote_item>*</to_quote_item>
        <to_invoice_item>*</to_invoice_item>
        <to_shipment_item>*</to_shipment_item>
        <to_cm_item>*</to_cm_item>
    </package>
    <tint>
        <to_quote_item>*</to_quote_item>
        <to_invoice_item>*</to_invoice_item>
        <to_shipment_item>*</to_shipment_item>
        <to_cm_item>*</to_cm_item>
    </tint>
</sales_convert_order_item>
Related Topic