3

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.

1

2 Answers 2

8

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
}
3
  • Does not work, getUsedProducts undefined. Jan 11, 2018 at 16:12
  • I need to get the minimum price of the child products
    – Jaisa
    Feb 21, 2018 at 9:32
  • @Rita Jose, I tried your code which is returning something other than the SKU of the first simple product of the configurable product. I was expecting it to return KY071597000 but it returned 2923. Could you please tell me what I am supposed to do? Apr 9, 2020 at 12:41
7

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();
2
  • $child->getId seems to return null, im not sure why, everything else like getName() and getDescription() return values normally Jun 7, 2017 at 21:50
  • $parentId = $this->registry->registry('current_product')->getId(); Sep 4, 2017 at 18:27

Your Answer

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

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