I am trying to fetch all child product Ids from configurable product Ids.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product = $objectManager->create('MagentoCatalogModelProduct');
$storeManager = $this->_objectManager->get('MagentoStoreModelStoreManagerInterface');
$currentStore = $storeManager->getStore();
$mediaUrl = $currentStore->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
$productURL = $mediaUrl.'catalog/product';
$collection = $this->_productCollectionFactory->create();
$productCollection = $collection->addAttributeToSelect('*')->addAttributeToFilter('type_id','configurable');
$vendor_product = array();
$productData = $product->getData();
foreach($productCollection as $prodObj){
$productData = array();
$product = $product->load($prodObj->getId());
$productTypeInstance = $product->getTypeInstance();
$usedProducts = $productTypeInstance->getUsedProducts($product);
foreach ($usedProducts as $child) {
$productData['childrenIds'][] = $child->getId();
}
I get Ids of my first configurable product in all the cases
5
Answers
$_children = $_product->getTypeInstance()->getUsedProducts($_product);
foreach ($_children as $child){
$childProducts[] = $child;
}
$_product is the configurable product object
You can use the below code for getting Child ID.
This is for sample code but I suggest do not use
$objectManage
r directly, you can use Block on Plugin Method to get Product Object.To get child product Ids from a configurable product, you can use below code:
Output looks like:
Hope it will help you.
To get all simple children including out of stock products use: