2

I have add a configure product in magento store which has associated products( simple and downloaded both) in admin. From frontend I add simple associated product in cart and get cart items information using following code:

$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach( $cart_items as $items )
{
   $items->getProductId();
}

At this time there is only one product in cart ( simple associated product ) but above code return id "95" and "96" where 95 for configurable product and 96 for simple associated product.

How to get only simple associated product id?

Thanks in advance.

3 Answers 3

1

You can also use $items->getParentItemId() to distinguish between children and parent items.

Parent items have a zero-value parent item id.

Inserting a configured product in cart will result in a parent quote item (referring to the configurable product) plus a child quote item (referring to the simple product which represents the user choice).

Hope it helps.

Kind regards, Alessandro

1
  • Thanks.. it works but there is a problem that $items->getQty(); always return "float(1)" while more than 1 quantity add in cart for child product.How to get correct quantity for child product? Commented Mar 5, 2015 at 4:09
1

When you invoke getAllItems(), it returns all quote items (parents and children). If you want just the items added (Magento adds the parent product when you add a child product) and shown in your cart, you need getAllVisibleItems().

$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();

This will return just the simple product you're looking for.

0
$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach( $cart_items as $items )
{
     if($itemts->getTypeId() != 'configurable')
     {
            $items->getProductId();
      }
}

the above should resolve your issue for you

3
  • 2
    Just a hint: use Product Type constants instead of hardcoded strings; you can find them in Mage_Catalog_Model_Product_Type Commented Mar 4, 2015 at 16:32
  • Normally i do this was for ease of clarity. Commented Mar 5, 2015 at 11:35
  • Thanks for give me a hint. but in my case if($items->getProductType() != 'configurable'){$productid = $items->getProductId();} is working. Commented Mar 9, 2015 at 6:41

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.