skip to Main Content

I am trying to fetch all child product Ids from configurable product Ids.

$objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
        $product = $objectManager->create('MagentoCatalogModelProduct');



        $storeManager = $this->_objectManager->get('MagentoStoreModelStoreManagerInterface'); 
        $currentStore = $storeManager->getStore();
        $mediaUrl = $currentStore->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
        $productURL = $mediaUrl.'catalog/product';
        $collection = $this->_productCollectionFactory->create();
        $productCollection = $collection->addAttributeToSelect('*')->addAttributeToFilter('type_id','configurable');
        $vendor_product = array();

        $productData = $product->getData();
        foreach($productCollection as $prodObj){
            $productData = array();
            $product = $product->load($prodObj->getId());

                     $productTypeInstance = $product->getTypeInstance();
            $usedProducts = $productTypeInstance->getUsedProducts($product);

 foreach ($usedProducts  as $child) {
$productData['childrenIds'][] = $child->getId();
}

I get Ids of my first configurable product in all the cases

5

Answers


  1. <?php
    
    namespace VendorModuleBlock;
    
    
    class ParentAndChilds extends MagentoFrameworkViewElementTemplate
    {
    
        /**
        * @var Context
        */
    
        protected $context;
    
        /**
        * @var ProductRepositoryInterface
        */
    
        protected $productRepository;
    
        /**
        * @var SearchCriteriaBuilder
        */
    
        protected $searchCriteriaBuilder;
    
        /**
         * @var LinkManagementInterface
         */
    
        protected $linkManagement;
    
    
        public function __construct(
            MagentoFrameworkViewElementTemplateContext $context,
            MagentoConfigurableProductApiLinkManagementInterface $linkManagement,
            MagentoCatalogApiProductRepositoryInterface $productRepository,
            MagentoFrameworkApiSearchCriteriaBuilder $searchCriteriaBuilder,
            array $data = []
    
        )
        {
            $this->linkManagement = $linkManagement;
            $this->productRepository = $productRepository;
            $this->searchCriteriaBuilder = $searchCriteriaBuilder;
            parent::__construct($context, $data);
        }
    
    
    
        public function getParentsAndChilds() 
        {
                $searchCriteria = $this->searchCriteriaBuilder
                    ->addFilter('type_id', 'configurable')
                    ->create();
    
                $configurableProducts = $this->productRepository
                    ->getList($searchCriteria);
    
                $parentAndChildProducts = array();
                foreach ($configurableProducts->getItems() as $configurableProduct) {
                    $childProducts = $this->linkManagement
                        ->getChildren($configurableProduct->getSku());
    
                    foreach ($childProducts as $childProduct) {
                        $parentAndChildProducts[$configurableProduct->getId()][] = $childProduct->getId();
                    }
                }
    
                return $parentAndChildProducts;
        }
    
    }
    
    Login or Signup to reply.
  2. $_children = $_product->getTypeInstance()->getUsedProducts($_product);
    foreach ($_children as $child){
    $childProducts[] = $child;
    }

    $_product is the configurable product object

    Login or Signup to reply.
  3. You can use the below code for getting Child ID.

    $product = $block->getProduct();
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $configurable_product_id = $product->getId();
    $configurableProduct = $objectManager->get('MagentoCatalogModelProductRepository')->getById($configurable_product_id);
    
    $children = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);       $childIds = array();
    foreach ($children as $child){
      $childIds[] = $child->getId();
    }
    sort($childIds);
    print_r($childIds);
    

    This is for sample code but I suggest do not use $objectManager directly, you can use Block on Plugin Method to get Product Object.

    Login or Signup to reply.
  4. To get child product Ids from a configurable product, you can use below code:

     public function __construct(
            MagentoCatalogApiProductRepositoryInterface $productRepository
        ) {
            $this->productRepository = $productRepository;
        }
    
    
        public function execute()
        { 
            $_productId=57786;
            $_product=$this->productRepository->getById($_productId);
            $childIs=$_product->getExtensionAttributes()->getConfigurableProductLinks();
    
            print_r($childIs);
        }
    

    Output looks like:

    Array
    (
        [31981] => 31981
        [31982] => 31982
        [31983] => 31983
    )
    

    Hope it will help you.

    Login or Signup to reply.
  5. To get all simple children including out of stock products use:

    $children = $product
              ->getTypeInstance()
              ->getChildrenIds($product->getId());
    
              print_r($children);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search