Get All simple product from a Configurable Product in Magento Product View

后端 未结 6 1564
轮回少年
轮回少年 2021-01-31 09:55

How can I get all the simple products associated with a configurable product? I found how to do the opposite (get a product configurable from a simple product) but that\'s not w

6条回答
  •  春和景丽
    2021-01-31 10:07

    write the below simplest code in block and just call it in your template file to get the products associated products :

        $productId = (int)$this->getRequest()->getParam('id');
        $objectManager = \Magento\Framework\App\objectManager::getInstance();
        $product = $objectManager->create("\Magento\Catalog\Model\Product")->load($productId);
        $collection = $product->getTypeInstance()->getAssociatedProducts($product);
        $this->setCollection($collection);
    

    now, in template write the below code to print the names :

       $collection = $block->getCollection(); 
       foreach ($collection as $key => $value) 
       {
          echo $value->getName();
          echo "
    "; }

提交回复
热议问题