Magento 1.9 – How to Get Chosen Attributes and Values of Configurable Products in Cart

cartconfigurable-productmagento-1.9product

I'm trying to list all the products in the cart, and list the chosen color/size, etc.. that they have. On the main website, Magento seems to do that. However, when I try to do it programmatically, I get the custom attributes as null.

This is my current code:

    $quote->getAllVisibleItems();

    foreach ($allCartItems as $item) {

        $productInfo = array();
        $product = $item->getProduct();
        $productInfo["size"] = $product->getSize();
        ...
    }

If I use getAllItems() instead of getAllVisibleItems(), then I will end up with simple/configurable products, which will cause an issue as to which item id the app has to use when updating the product (as well as listing duplicate items).

The only solution I found online was to load the product again using $product = $model->load($model->getIdBySku($sku)); for every product listed, but that seems to be like it will quite the performance hog!

How can I have the getAllVisibleItems() return the chosen attributes and attribute values of the configurable products it returns?

enter image description here

[
    {
    name: "Tori Tank",
    price: "60.0000",
    id: "418",
    product_id: "418",
    item_id: "2481",
    sku: "wbk004",
    color: null,
    size: null,
    quantity: 1
    },
    {
    name: "Chelsea Tee",
    price: "75.0000",
    id: "410",
    product_id: "410",
    item_id: "2483",
    sku: "mtk005",
    color: null,
    size: null,
    quantity: 1
    }
]

As you can see, the color/size are being returned as null.

Best Answer

Quick solution:

$item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());

Explanation:

You have an issue because when you run $item->getProduct(); you just receive this configurable product. Its the same if you run Mage::getModel('catalog/product')->load($someConfigurableProductId)

Related Topic