How To

How to Get Associated Product Information from Grouped Product SKU in Magento 2

Hello Magento Friends,

Today I am going to explain How to Get Associated Product Information from Grouped Product SKU in Magento 2.

Grouped products are a set of simple products combined together in order to increase purchase value. You can Create Grouped Product Programmatically in Magento 2.

When you need the information of the associated product of a particular grouped product you can apply the below steps. With the help of the below code, you can get information like Product ID, name, price, etc.

Steps to Get Associated Product Information from Grouped Product SKU in Magento 2:

Step 1: Go to the below file path

app\code\Vendor\Extension\Controller\Index\Groupproductset.php

Now, add the below code

namespace Vendor\Extension\Controller\Index;

use Magento\Catalog\Model\Product;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Groupproductset extends Action
{
    protected $_productCollection;

    public function __construct(
    Product $_productCollection,
    Context $context)
    {
        $this->_productCollection = $_productCollection;
        parent::__construct($context);
    }

     public function execute()
    {
 $group_product_sku = “group_product_sku”;
 $productId = $this->_productCollection->getIdBySku($group_product_sku);
 $groupProduct = $this->_productCollection->load($productId);
 $groupProduct_name = $groupProduct->getName();

 $_children = $groupProduct->getTypeInstance(true) ->getAssociatedProducts($groupProduct);

 $simpleproduct = array();

 foreach ($_children as $child)
        {
             if ($child->getId() != $groupProduct->getId())
                {
              echo $child->getSKU();
              echo $child->getPrice();
              echo $child->getName();
             }
        }
    }
}

Step 2: After adding the above file run the below commands

sudo php bin/magento setup:di:compile
sudo php bin/magento cache:flush

Conclusion:

This way you can get the associated product from the grouped product ID in Magento 2. If you face any trouble, feel free to share it with me via the comment part. Share the article and stay in touch with us.

Happy Coding!

Click to rate this post!
[Total: 4 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

5 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago