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 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.
Please check my answer and let me know.
– Rohan Hapani
Sep 13 '18 at 8:57