Site icon MageComp Blog

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

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!

Exit mobile version