Configurable Product - Get child product id

Configurable Product - Get child product id



How to get child product id's from parent product id in magento2 for configurable product?



I want to get child product id of parent product in magento based on parent product id.






Please check my answer and let me know.

– Rohan Hapani
Sep 13 '18 at 8:57






Please accept and upvote answer.

– Rohan Hapani
Sep 13 '18 at 9:50




2 Answers
2



Try the below solution:


<?php
$productId = 5; //Configurable product ID
$_objectManager = MagentoFrameworkAppObjectManager::getInstance();
$_product = $_objectManager->create('MagentoCatalogModelProduct')->load($productId);
$_childProducts = $_product->getTypeInstance()->getUsedProducts($_product);
foreach ($_childProducts as $simpleProduct)
echo $simpleProduct->getId();

?>



Suggestion: Don't use object manager directly in your code as its not a best practice. You need to inject the product model class to your respective class then use it.






yes i have injected model and working fine. Thanks @sukumar

– Sanjay Vadadoriya
Sep 13 '18 at 11:01






Welcome. Happy Coding :)

– Sukumar Gorai
Sep 13 '18 at 11:03



Try to use this code :


$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$configProduct = $objectManager->create('MagentoCatalogModelProduct')->load($product_id);

$_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);

foreach ($_children as $child)
echo $child->getID();



Thanks for contributing an answer to Magento Stack Exchange!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)