1

I know to load children skus with loading parent object but I am finding the way where I can load all children items by just passing configurable product id, is there any method please share here thank you it works fine with below method but it requires to load parent object

$configurableProduct=$this->productRepository->getById($parentId);
//\Magento\ConfigurableProduct\Model\Product\Type\Configurable
$options = $this->configurable->getConfigurableOptions($configurableProduct);  

2 Answers 2

0

Please try following code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$configProduct = $objectManager->create('Magento\Catalog\Model\Product')->load($parentId);

$_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);
$getChildSku = array();
foreach ($_children as $child){    
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($child->getID());
    $getChildSku[] = $product->getSku();
}
echo "<pre>";
print_r($getChildSku);
echo "</pre>"; 

Manage objectManager yourself

3
  • you don't need the load in the foreach loop. you can just call $child->getSku(). It's faster
    – Marius
    Commented Jun 9, 2023 at 12:33
  • Yes, We can get sku without load. Thanks @Marius
    – MP Raj
    Commented Jun 9, 2023 at 13:13
  • @MPRaj I already mentioned I do NOT want to load parent object, I need to get child skus without load parent object Commented Jun 9, 2023 at 15:18
0

I hope this works for you

public function __construct(     
    \Magento\Catalog\Model\ProductFactory $_productloader
) {
    $this->_productloader = $_productloader;
}

public function getLoadProduct()
{
    // $product_id=7; // configurable product id
    // $configProduct=$this->_productloader->create()->load($product_id);
    $configProduct = 'your config product object'; // your config product object
    $_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);

    foreach ($_children as $child) {
        echo "Child Product Skus ".$child->getSku()".<br />;
    }
}
1
  • thank you for answer, but please read question Commented Nov 7, 2023 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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