11

I'm trying to load the simple products that have been added to a customer's cart, but when I retrieve the items, it's showing the parent configurable.

$cart = Mage::getSingleton('checkout/cart');
$productIds = array();

foreach ($cart->getQuote()->getAllVisibleItems() as $item) {
    $productIds[] = $item->getProduct()->getId();
}

var_dump($productIds);

For instance, this will return all the same configurable id when I've added a small, medium, and large to my cart. How can I get the individual simple products? I'm trying to retrieve an attribute value that's set on the simple product level.

1 Answer 1

24

After taking a look at how Magento renders the items in your cart on the checkout/cart page, I was able to find this in app/code/core/Mage/Checkout/Block/Cart/Item/Renderer/Configurable.php

/**
 * Get item configurable child product
 *
 * @return Mage_Catalog_Model_Product
 */
public function getChildProduct()
{
    if ($option = $this->getItem()->getOptionByCode('simple_product')) {
        return $option->getProduct();
    }
    return $this->getProduct();
}

So, applying it to the snippet in the question, it would be

foreach ($cart->getQuote()->getAllVisibleItems() as $item) {
    $productId = $item->getProduct()->getId();
    if ($option = $item->getOptionByCode('simple_product')) {
        $productId = $option->getProduct()->getId();
    }
    $productIds[] = $productId;
}
1
  • 15
    For others : A one line code : $_item->getOptionByCode('simple_product')->getProduct()->getId();
    – Nikhil_K_R
    Commented Nov 19, 2013 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.