You have to just keep below code inside block file and call function inside your template,
protected $productFactory;
protected $dataObjectHelper;
protected $productRepository;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
array $data = []
){
$this->productFactory = $productFactory;
$this->productRepository = $productRepository;
$this->dataObjectHelper = $dataObjectHelper;
parent::__construct($context,$data);
}
public function getConfigChildProductIds($id){
$product = array();
if(is_numeric($id)){
$product = $this->productRepository->getById($id);
}else{
return;
}
if(!isset($product)){
return;
}
if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
return [];
}
$storeId = $this->_storeManager->getStore()->getId();
$productTypeInstance = $product->getTypeInstance();
$productTypeInstance->setStoreFilter($storeId, $product);
$usedProducts = $productTypeInstance->getUsedProducts($product);
$childrenList = [];
foreach ($usedProducts as $child) {
$attributes = [];
$isSaleable = $child->isSaleable();
//getting in-stock product
if($isSaleable){
foreach ($child->getAttributes() as $attribute) {
$attrCode = $attribute->getAttributeCode();
$value = $child->getDataUsingMethod($attrCode) ?: $child->getData($attrCode);
if (null !== $value && $attrCode != 'entity_id') {
$attributes[$attrCode] = $value;
}
}
$attributes['store_id'] = $child->getStoreId();
$attributes['id'] = $child->getId();
/** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
$productDataObject = $this->productFactory->create();
$this->dataObjectHelper->populateWithArray(
$productDataObject,
$attributes,
'\Magento\Catalog\Api\Data\ProductInterface'
);
$childrenList[] = $productDataObject;
}
}
$childData = array();
foreach($childrenList as $child){
$childData[] = $child;
}
return $childData;
}
Inside template file call ,
$productId = 10 //parent product id..
$childObj = $block->getConfigChildProductIds($productId);
foreach ($childObj as $child){
$getChildId[] = $child->getData();
}
echo "<pre>";print_r($getChildId);