How to Get Estimated Shipping Methods using REST API in Magento 2


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>';

 

?>

Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Update Product Stock Programmatically in Magento 2

How to Set and Get Cookie in Magento 2