How to get associated(child) product id, name and other details using configurable product id in Magento 2
-
09-10-2020 - |
Question
I need to get all the child products details like id, name, color from a configurable product id in a custom phtml page.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$productTypeInstance = $product->getTypeInstance();
$usedProducts = $productTypeInstance->getUsedProducts($product);
echo $product->getId(); //Main configurable product ID
echo $product->getName(); //Main Configurable Name
foreach ($usedProducts as $child) {
echo $child->getId()."</br>"; //Child Product Id
echo $child->getName()."</br>"; //Child Product Name
}
Used the above code. But error occurs for getUsedProducts. please help me to find a solution.
Solution
Will get the answer by this code itself afeter di:compile
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$productTypeInstance = $product->getTypeInstance();
$usedProducts = $productTypeInstance->getUsedProducts($product);
echo $product->getId(); //Main configurable product ID
echo $product->getName(); //Main Configurable Name
foreach ($usedProducts as $child) {
echo $child->getId()."</br>"; //Child Product Id
echo $child->getName()."</br>"; //Child Product Name
}
OTHER TIPS
Firstly, never use objectManager like you did in your phtml or in any block class. You should always use automatic constructor's dependency injection system.
I suggest you to utilise Magento 2's Service Contract, if you'd like to do it Magento way. There might be lots of methods available ported from Magento 1 but those will be removed in future updates - 'getUsedProducts' is one of them.
Add the following code in your block class:
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\ConfigurableProduct\Api\LinkManagementInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Registry;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
/**
* @var LinkManagementInterface
*/
protected $linkManagement;
/**
* @var LinkManagementInterface
*/
protected $registry;
/**
* @param LinkManagementInterface $linkManagement
* @param ProductRepositoryInterface $productRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param Registry $registry
*/
public function __construct(
LinkManagementInterface $linkManagement,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
Registry $registry
) {
$this->linkManagement = $linkManagement;
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->registry = $registry;
parent::__construct();
}
public function getChilds()
{
$parentId = $this->registry->registry('current_product')->getId();
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('type_id', 'configurable')
->addFilter('entity_id', $parentId)
->create();
$configurableProducts = $this->productRepository
->getList($searchCriteria);
foreach ($configurableProducts->getItems() as $configurableProduct) {
$childProducts = $this->linkManagement
->getChildren($configurableProduct->getSku());
}
return $childProducts;
}
Now you can get the $childProducts collection in your blocks phtml something like:
$childProducts = $block->getChilds();
foreach ($childProducts as $child) {
$childId = $child->getId();
}
echo $childId();