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

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

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

  • Thanks a lot Marius, from where are you reading Magento2, you are too quick :) – Ashutosh Pandey Jan 14 '16 at 12:00
  • 1
    I don't read it. I just played with it for a while – Marius Jan 14 '16 at 12:13
2

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.