Magento 2 Get Shipping Information Details Via REST API

I am going to explain to you in detail to get the Shipping Information Details available in the Magento 2 store through rest api.

Through this API you can easily get the shipping information details available on any Magento 2 website.

You can also get the idea about what would be the endpoint of that API, What methods will we use when we hit the API and what should be its body details and what payloads should be sent.

You will get all these details through the rest API post in detail.

 

Magento 2 Get Shipping Information Details API

By using the following endpoint you can get the shipping information details through this rest API in Magento 2 store.

Before reaching this step we need to add some items inside our cart then after that, we will process our order toward the checkout.

  1. Estimate shipping costs
  2. Set shipping and billing information
  3. Estimate shipping costs

There is a separate cost for each shipping method which is calculated at the time of checkout of the order.

When we move towards check out, adding this cost inside the subtotal gives us the final price.

Through this post, we will get those steps with detailed information.

 

Endpoint:

https://www.your_domain_name.com/V1/guest-carts/<cartId>/estimate-shipping-methods

Headers:

  • Content-Type: application/json
  • Authorization: Bearer <customer token>

Payload:

{
"address": {
"region": "Uttar Pradesh",
"region_id": 566,
"region_code": "UP",
"country_id": "IN",
"street": [
"Noida"
],
"postcode": "302022",
"city": "Noida",
"firstname": "Hemant",
"lastname": "Singh",
"customer_id": 111,
"email": "cs.hemantsingh@gmail.com",
"telephone": "(512) 555-1111",
"same_as_billing": 1
}
}

 

Response:

When we calculate the price at the checkout step of a product order, some other charges like shipping method charges flat-rate shipping, fixed price, etc. are also included.

But this charge is not applicable for downloadable products as the customer downloads the same.

Because this product does not need to be delivered, due to that shipping charges are not levied.

[
{
"carrier_code": "flatrate",
"method_code": "flatrate",
"carrier_title": "Flat Rate",
"method_title": "Fixed",
"amount": 15,
"base_amount": 15,
"available": true,
"error_message": "",
"price_excl_tax": 15,
"price_incl_tax": 15
},
{
"carrier_code": "tablerate",
"method_code": "bestway",
"carrier_title": "Best Way",
"method_title": "Table Rate",
"amount": 5,
"base_amount": 5,
"available": true,
"error_message": "",
"price_excl_tax": 5,
"price_incl_tax": 5
}
]

 

Steps to Save Shipping And Billing Information Via API

Now you can save both shipping address and billing address by using the following endpoint.

For this, you have to hit on the below endpoint and send a payload.

Inside that payload, you will mention both your shipping address and billing address and also send the customer token as authorization inside the header.

You have to also select carrier_code and method_code along with shipping and billing details in the payload.

Now the pricing will be calculated as a total and sent you the response of the order along with the payment method.

 

Read more: Magento 2.4 Installation Guide: How to Install Magento 2.4.2

 

Guest Carts Endpoint:

https://www.your_domin_name.com/V1/guest-carts/<cartId>/shipping-information

This guest cart endpoint will help you to set the billing and shipping information on behalf of a guest.

Login Customer Endpoint:

https://www.your_domin_name.com/<host>/rest/<store_code>/V1/carts/mine/shipping-information

Method: POST

Headers:

  • Content-Type: application/json
  • Authorization: Bearer <customer token>

Body Payload:

{
"addressInformation": {
"shipping_address": {
"region": "Uttar Pradesh",
"region_id": 566,
"region_code": "UP",
"country_id": "566",
"street": [
"Noida"
],
"postcode": "201301",
"city": "Noida",
"firstname": "Hemant",
"lastname": "Singh",
"email": "cs.hemantsingh@gmail.com",
"telephone": "7992293862"
},
"billing_address": {
"region": "Uttar Pradesh",
"region_id": 566,
"region_code": "UP",
"country_id": "IN",
"street": [
"Noida"
],
"postcode": "201301",
"city": "Noida",
"firstname": "Hemant",
"lastname": "Singh",
"email": "cs.hemantsingh@gmail.com",
"telephone": "7992293862"
},
"shipping_carrier_code": "tablerate",
"shipping_method_code": "bestway"
}
}

Response:

Now Magento store will send you the subtotal of the order, including shipping charges. Now the grand total will be subtotal+shipping charge.

The available payment method enabled inside the Magento 2 website will be shown to you as a response, you can make the payment using that payment method.

Shipping Information Detail

{
"payment_methods": [
{
"code": "cashondelivery",
"title": "Cash On Delivery"
},
{
"code": "banktransfer",
"title": "Bank Transfer Payment"
},
{
"code": "purchaseorder",
"title": "Purchase Order"
},
{
"code": "checkmo",
"title": "Check / Money order"
}
],
"totals": {
"grand_total": 165,
"base_grand_total": 165,
"subtotal": 160,
"base_subtotal": 160,
"discount_amount": 0,
"base_discount_amount": 0,
"subtotal_with_discount": 160,
"base_subtotal_with_discount": 160,
"shipping_amount": 5,
"base_shipping_amount": 5,
"shipping_discount_amount": 0,
"base_shipping_discount_amount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"weee_tax_applied_amount": null,
"shipping_tax_amount": 0,
"base_shipping_tax_amount": 0,
"subtotal_incl_tax": 160,
"shipping_incl_tax": 5,
"base_shipping_incl_tax": 5,
"base_currency_code": "USD",
"quote_currency_code": "USD",
"items_qty": 4,
"items": [
{
"item_id": 6,
"price": 22,
"base_price": 22,
"qty": 1,
"row_total": 22,
"base_row_total": 22,
"row_total_with_discount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"tax_percent": 0,
"discount_amount": 0,
"base_discount_amount": 0,
"discount_percent": 0,
"price_incl_tax": 22,
"base_price_incl_tax": 22,
"row_total_incl_tax": 22,
"base_row_total_incl_tax": 22,
"options": "[]",
"weee_tax_applied_amount": null,
"weee_tax_applied": null,
"name": "Radiant Tee-M-Orange"
},
{
"item_id": 7,
"price": 18,
"base_price": 18,
"qty": 1,
"row_total": 18,
"base_row_total": 18,
"row_total_with_discount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"tax_percent": 0,
"discount_amount": 0,
"base_discount_amount": 0,
"discount_percent": 0,
"price_incl_tax": 18,
"base_price_incl_tax": 18,
"row_total_incl_tax": 18,
"base_row_total_incl_tax": 18,
"options": "[{\"value\":\"Advanced Pilates & Yoga (Strength)\",\"label\":\"Downloads\"}]",
"weee_tax_applied_amount": null,
"weee_tax_applied": null,
"name": "Advanced Pilates & Yoga (Strength)"
},
{
"item_id": 8,
"price": 68,
"base_price": 68,
"qty": 1,
"row_total": 68,
"base_row_total": 68,
"row_total_with_discount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"discount_amount": 0,
"base_discount_amount": 0,
"discount_percent": 0,
"price_incl_tax": 68,
"base_price_incl_tax": 68,
"row_total_incl_tax": 68,
"base_row_total_incl_tax": 68,
"options": "[{\"value\":\"1 x Sprite Stasis Ball 65 cm <span class=\\\"price\\\">$27.00<\\/span>\",\"label\":\"Sprite Stasis Ball\"},{\"value\":\"1 x Sprite Foam Yoga Brick <span class=\\\"price\\\">$5.00<\\/span>\",\"label\":\"Sprite Foam Yoga Brick\"},{\"value\":\"1 x Sprite Yoga Strap 8 foot <span class=\\\"price\\\">$17.00<\\/span>\",\"label\":\"Sprite Yoga Strap\"},{\"value\":\"1 x Sprite Foam Roller <span class=\\\"price\\\">$19.00<\\/span>\",\"label\":\"Sprite Foam Roller\"}]",
"weee_tax_applied_amount": null,
"weee_tax_applied": null,
"name": "Sprite Yoga Companion Kit"
},
{
"item_id": 13,
"price": 52,
"base_price": 52,
"qty": 1,
"row_total": 52,
"base_row_total": 52,
"row_total_with_discount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"tax_percent": 0,
"discount_amount": 0,
"base_discount_amount": 0,
"discount_percent": 0,
"price_incl_tax": 52,
"base_price_incl_tax": 52,
"row_total_incl_tax": 52,
"base_row_total_incl_tax": 52,
"options": "[{\"value\":\"Gray\",\"label\":\"Color\"},{\"value\":\"S\",\"label\":\"Size\"}]",
"weee_tax_applied_amount": null,
"weee_tax_applied": null,
"name": "Chaz Kangeroo Hoodie"
}
],
"total_segments": [
{
"code": "subtotal",
"title": "Subtotal",
"value": 160
},
{
"code": "shipping",
"title": "Shipping & Handling (Best Way - Table Rate)",
"value": 5
},
{
"code": "tax",
"title": "Tax",
"value": 0,
"extension_attributes": {
"tax_grandtotal_details": []
}
},
{
"code": "grand_total",
"title": "Grand Total",
"value": 165,
"area": "footer"
}
]
}
}

 

Verify This Steps

Now, in order to verify this is to get the details you have to log in as a customer of the Magento 2 store and then go to the checkout page.

You can see that the payment method and other details in which you have saved the data through REST API are showing.

 

Conclusion:

It is possible to save shipping information or gate payment information through REST API.

You can get the shipping method details of any customer through the above-mentioned API and save shipping details.

 

Recommended Posts:

Guest Cart Paypal Payment Steps: Magento 2 REST API

Get Customer Detail Using Email ID in Magento 2 REST API