Today’s tutorial discusses how to create a guest order using the REST API in Magento 2. Oftentimes, we have to integrate our store into mobile applications, and mobile application needs the data in the form of APIs.
Following are the steps to create a guest order using Rest API in Magento 2
Step 1: Create the Cart
POST https://[urltowebiste]/rest/[store_code>]/V1/guest-carts
There is no payload for this endpoint, and it creates a token for your cart Id. The next step would be adding products to this empty cart.
Step 2: Add items to the already created cart
POST https://[urltowebiste]/rest/[store_code>]/V1/guest-carts/[quote_token]/items
{
"cart_item": {
"quote_id": "uDDZtC3rSmqBY56myV5aYWwYDSnbTIuf",
"sku": "Your Product SKU",
"qty": 1
}
}
Send the payload to this above URL, and your cart will be updated with the above SKU
Step 3: Check the available Shipping methods
POST https://[urltowebiste]/rest/[store_code]/V1/guest-carts/[quote_token]/estimate-shipping-methods
{
"address":
{
"region": "NewYork",
"country_id": "US",
"street": [
"L-142, 5th Main Rd"
],
"postcode": "123456",
"city": "NewYork",
"firstname": "First Name",
"lastname": "Last",
"customer_id": null,
"email": "[email protected]",
"telephone": "12345678",
"same_as_billing": 1
}
}
The above endpoint will provide the available shipping method details, for the selected country and region. Keep these records handy to proceed with the next steps.
Step 4: Get the available payment methods
POST https://[urltowebiste]/rest/[store_code]/V1/guest-carts/[quote_token]/shipping-information
{
"addressInformation": {
"shipping_address": {
"region": "NewYork",
"region_id": 0,
"region_code": "",
"country_id": "US",
"street": [
"123 Oak Ave"
],
"postcode": "10577",
"city": "Purchase",
"firstname": "First Name",
"lastname": "Last",
"email": "[email protected]",
"telephone": "12345678"
},
"billing_address": {
"region": "NewYork",
"region_id": 0,
"region_code": "",
"country_id": "US",
"street": [
"123 Oak Ave"
],
"postcode": "10577",
"city": "Purchase",
"firstname": "First Name",
"lastname": "Last",
"email": "[email protected]",
"telephone": "12345678"
},
"shipping_carrier_code": "freeshipping",
"shipping_method_code": "freeshipping"
}
}
Fill in the shipping details from the previous API, and fill in other necessary details, and it will output the available payment methods.
Step 5: Place the order
POST https://[urltowebiste]/rest/[store_code]/V1/guest-carts/[quote_token]/payment-information
{
"email": "[email protected]",
"paymentMethod": {
"method": "cashondelivery"
},
"billing_address": {
"email": "[email protected]",
"region": "NewYork",
"region_id": 0,
"region_code": "",
"country_id": "US",
"street": [
"123 Oak Ave"
],
"postcode": "1234",
"city": "NewYork",
"telephone": "12345678",
"firstname": "First Name",
"lastname": "Last"
}
}
The above endpoint will generate the order id as output.
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply