2

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.

5

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

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
}
  • Does not work, getUsedProducts undefined. – Kapil Yadav Jan 11 '18 at 16:12
  • I need to get the minimum price of the child products – Jaisa Feb 21 '18 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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