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

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

43 mins ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

5 days ago