How To

How to Get Child Product of Configurable Product using REST API in Magento 2?

Hello Magento Friends,

In today’s blog post, I am going to explain How to Get Child Product of Configurable Product using REST API in Magento 2.

Magento supports various product types and one of them is Configurable Products. When you need to provide options for a single product, you need to create configurable products in Magento 2.

Configurable products have various child products. Say, for example, you are selling a t-shirt with a t-shirt design maker and you want to provide color and size options to the customers. Then, the t-shirt is a configurable product while color and size options are its child products.

Let’s see How to Get Child Product of Configurable Product using REST API in Magento 2.

Steps to Get Child Product of Configurable Product using REST API in Magento 2:

Step 1: Create a getchildproductinfo.php file anywhere and add the following code

<?php
$baseUrl = "http://yourdomain"; 
$userData = array("username" => "admin", "password" => "admin@123");

$ch = curl_init($baseUrl."/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);
$ch = curl_init($baseUrl."/rest/all/V1/configurable-products/WSH01/children");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';
print_r($result);
echo '</pre>';
?>

Conclusion:

This way you can retrieve child products of configurable product with the help of REST API in Magento 2. If you have any doubts about the above code let me know through the comments. Share the article further and stay updated with us 

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
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

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…

6 days 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

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago