How To

Magento 2: How to Get Estimated Shipping Methods using REST API?

Hello Magento Friends,

Today I came up with a new Magento solution for you all. It is about How to Get Estimated Shipping Methods using REST API in Magento 2.

Admin can configure multiple shipping methods in Magento 2 to provide a better user experience to customers.

Magento 2 provides default shipping methods like 

Among the above list, flat rate shipping is the most popular one. However, you can only create one flat rate shipping method in default Magento 2. Multiple Flat Rate Shipping Extension will help you configure more than one flat rate shipping. 

If you do not want to use default shipping methods, you can even Create Custom Shipping Method in Magento 2.

You can get the shipping methods using the REST API in Magento 2. Let’s look at the steps

Steps to Get Estimated Shipping Methods using REST API in Magento 2:

Step 1: Create the rootscript.php script file and add the below code

<?php
 
$baseUrl = " http://yourdomain/"; // your magento base url
//Customer Email id and Password set
 
$userData = array("username" => "test@gmail.com", "password" => "test@123");
 
$ch = curl_init($baseUrl."/rest/V1/integration/customer/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);
 
$shippingData = array("address" => 
  array("city" => "THOUSAND OAKS",
        "company" => "Test Company",
        "country_id" => "US", 
        "customer_id" => 0,
        "firstname"=>"Michael S",
        "id"=> 8,
        "lastname"=> "Yarbrough",
        "postcode" => "91358",
        "region_code"=> "CA",
        "region"=> "California", 
        "region_id"=> 12, 
        "street" => ["721 Hall Street"],
        "telephone" => "159634877"));

$ch = curl_init($baseUrl."/rest/V1/carts/mine/estimate-shipping-methods");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($shippingData));
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>';
 
?>

After successfully implementing the above step, it will return the shipping methods in array as follows.

Conclusion:

This way you can Get Estimated Shipping Methods using REST API in Magento 2. Alternatively, Get Shipment Information using SOAP API in Magento 2. If you face any hardships with the above code snippet, you can ask me through the comment part. Share the solution with your contacts. See you with the next Magento solution.

Happy Coding!

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

5 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