2

I'm new to Magento2 and finding it difficult to get information. I'm trying to read simple (variants) products of a configurable product on the view page.

I created my custom block that extends AbstractProduct.

Then I was able to get upsell product ids like this:

$product = $this->_coreRegistry->registry('product');
$upSellProductIds = $product->getUpSellProductIds();

Now, how to get variants(simple products).

In Magento 1.x, I used getUsedProductCollection()

Please help

1
  • can you please show your working example I need the same but I can't get workin
    – fefe
    Apr 14 '16 at 12:04
14

Try with $product->getTypeInstance()->getUsedProducts($product).

1
  • should check condition if product is configurable then getUsedProducts() medthod work otherwise throw error.
    – HaFiz Umer
    Feb 26 '20 at 12:31
3

You can get child product of current configurable product by this simple code:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    $productTypeInstance = $product->getTypeInstance();
    $usedProducts = $productTypeInstance->getUsedProducts($product);

    echo $product->getId(); //Main configurable product ID
    echo $product->getName(); //Main Configurable Name

    foreach ($usedProducts  as $child) {
        echo $child->getId()."</br>"; //Child Product Id  
        echo $child->getName()."</br>"; //Child Product Name
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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