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
Why would you do this ?? In case somebody will create this class with method in his codebase and then use in other class where it’s really needed, this class wouldn’t be necessary at all.
From my point of view, you should write:
Use in your PHP class:
$this->configurable->getChildrenIds($id)
… and add to constructor
$this->configurable = $configurable;
… and into constructor arguments
MagentoConfigurableProductModelProductTypeConfigurable $configurable
Don’t get me wrong, I like your work and spreading Magento knowledge, but I’ve seen a lot of code when developers just copy/paste code to their codebase. And we don’t want to see not necessary classes in Magento codebase ?