2

I am working with configurable product, I want simple product id of configurable product on cart.phtml, I am using this code

 <?php foreach($this->getItems() as $_item): ?>
$_product = $_item->getProduct();
echo $_product->getId();
 <?php endforeach ?>

but it always gives main product id, but I need its simple product id , any one help please.

4 Answers 4

10

In quote item table magento have sku and product id. For Configurable product's cart , 'sku' is simple product sku and product id is configurable product 'id'. SO, you need to load product by sku

<?php foreach($this->getItems() as $_item): ?>
 $_Configproduct = $_item->getProduct();
 $simpleProduct=Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku());
echo $simpleProduct->getId();
<?php endforeach ?
4
  • This wouldn't work. $_item in your example is still the configurable product and would end up loading the configurable product id once more.
    – Ian
    May 6, 2014 at 15:45
  • ok,let checks .
    – Amit Bera
    May 6, 2014 at 15:46
  • try echo $_item->getSku() , check what is return??
    – Amit Bera
    May 6, 2014 at 15:48
  • I forgot hat it stores the simple SKU for both configurable & simple in quote table. I was wrong, it would load the correct simple product ID.
    – Ian
    May 6, 2014 at 15:51
5

Try this : $_item->getOptionByCode('simple_product')->getProduct()->getId()

Source : https://stackoverflow.com/questions/18027524/get-simple-product-from-configurable-in-cart

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;
}
2

So, $this->getItems() actually calls:

$this->getQuote()->getAllVisibleItems();

Which only returns products you actually see on the cart page (configurable, stand-alone simple, bundle, etc).

You need the simple. So here we go (This code I tested in checkout/cart/item/default.phtml).

if ($_item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
{
      //Return first item of the array, in the case of 'configurable', it will be a one item array.
      $_simpleItem = reset($_item->getChildren());

      //print out simple id
      echo $_simpleItem->getProductId();
}
2
  • Or you could do $_item->getChildrenIds() to get all IDs of the associated simple products.
    – laketuna
    May 6, 2014 at 15:40
  • You could, but I think my method is more powerful for no extra work. What are you going to do with that product ID? Load the full product to get information off of it? You don't need to as now you can "$_simpleItem->getProduct()" and you have the full product ready to go. But yes, you are correct; that method would work as well depending on what you needed.
    – Ian
    May 6, 2014 at 15:45
0

Magento itself provides the solution of getting the simple product object from configurable in the following file class:

Mage_Checkout_Block_Cart_Item_Renderer_Configurable

function name is getChildProduct() which returns the simple product object from configurable one.

public function getChildProduct()
{
    if ($option = $this->getItem()->getOptionByCode('simple_product')) {
        return $option->getProduct();
    }
    return $this->getProduct();
}

Your Answer

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

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