How to Get Configurable product children ids Magento 2?

Get all the children product ids from the Configurable product in Magento 2. You can retrieve the list of child item ids by the configurable product id.

Get all Children items of Bundle Products Magento 2.

To fetch all the children items from the configurable product, get Configurable product id and pass it to the method in given code,

<?php
namespace Jesadiya\ChildIds\Model;

use Magento\ConfigurableProduct\Model\Product\Type\Configurable;

class ConfigurableChildrenIds
{
    /**
     * @var Configurable
     */
    private $configurable;

    public function __construct(
        Configurable $configurable
    ) {
        $this->configurable = $configurable;
    }

    /**
     * Get Children items id by the parent id
     *
     * @param int $id
     *
     * @return array
     */
    public function getChildrenIds(int $id)
    {
        $childItemId = $this->configurable->getChildrenIds($id);
        return $childItemId;
    }
}

Call from the PHP class,

$configurableId = 2046;
$superAttributeByChild = $this->getChildrenIds($configurableId);

List of all the children items id for a configurable product.

Output:
Array

How to Get Child Configurable product Super attribute value Magento 2?

You can retrieve the Children item used super attribute label value data from the child and parent product SKU of the configurable product.

This article is useful when you are programmatically adding a Configurable product to the cart. 

Let’s take an example, You need to fetch used super attribute value of the child item, native Magento sample data configurable product name, Cassius Sparring Tank has product SKU MT12 and its child product name Cassius Sparring Tank-XL-Blue has product SKU MT12-XL-Blue. Continue reading “How to Get Child Configurable product Super attribute value Magento 2?”

Magento 2 Get Parent Product id from child id.

Magento 2 Get Configurable product id from Child id of configurable. When you have Child id of the P product is available you can simply get the parent config product id.

Create simple block to get parent product id,

<?php
namespace Rbj\Product\Block;

class Product extends \Magento\Framework\View\Element\Template
{
	public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
        array $data = []
    ) {
        $this->configurable = $configurable;
        parent::__construct($context, $data);
    }

    /**
     * @param int $childId
     * @return int
     */
    public function getParentProductId($childProductId)
    {
            $parentConfigObject = $this->configurable->getParentIdsByChild($childProductId);
	    if($parentConfigObject) {
		return $parentConfigObject[0];
	    }
	    return false;
    }
}

now call the function in template file to get Parent Id,

$childId = 10;
echo $block->getParentProductId($childId);