Which steps are required to place an order for a Guest Customer by REST API?
You must follow the steps to place an order without creating a customer account. You can add items to your cart and place an order with the Guest Customer.
You have to follow six Steps for the REST API to Place an order by Guest Customer,
1) Create Guest Empty Cart
2) Get Cart Id by token(masked_id) return from Step 1.
3) Add an Item to the Cart for the Guest.
4) Fetch estimate-shipping-methods for the cart.
5) Fetch the available Payment method for the cart.
6) Place an Order for the Guest customer.
Step 1) Create Guest Empty Cart
Guest Customers must create an empty cart(quote) with the REST API. The response will be a cart_id that will be passed to a subsequent API to place an order.
Action: POST
API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts
(Example: https://magento.test/rest/default/V1/guest-carts)
Payload: Empty
Response: w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ
Step 2) Get Cart Id by token(masked_id) return from Step 1.
In the second step, you can get the quote ID by calling API, you have to replace :cartId with response return in step 1.
Action: GET
API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId
(Example: https://magento.test/rest/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ)
Payload: Empty
Response:
{ "id": 1187676, "created_at": "2022-07-05 04:23:12", "updated_at": "2022-07-05 04:23:12", "is_active": true, "is_virtual": false, "items": [], "items_count": 0, "items_qty": 0, "customer": { "email": null, "firstname": null, "lastname": null }, "billing_address": { ..... } }
Step 3) Add an Item to the Cart for the Guest.
For the third step, you have cart_id(w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ) and quote_id(1187676) from the step 1 and 2.
You have to add items to the cart using REST API. you can able to add single or multiple items to the cart with API.
Action: POST
API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId/items
(Example: https://magento.test/rest/default/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ/items)
Payload:
{ "cartItem": { "quote_id": "1187676", "sku": "24-MB01", "qty": 1 } }
Response:
{ "item_id": 1448307, "sku": "24-MB01", "qty": 1, "name": "Item Bag", "price": 7.99, "product_type": "simple", "quote_id": "1187676" }
Step 4) Fetch estimate-shipping-methods for the cart.
In step 4, you need to fetch available shipping methods from the shipping address you have passed in the payload.
The response will be an available shipping method for the cart.
Action: POST
API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId/estimate-shipping-methods
(Example: https://magento.test/rest/default/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ/estimate-shipping-methods)
Payload:
{ "address": { "region": "Dublin", "country_id": "IE", "street": [ "123 Oak Ave" ], "postcode": "W34 X5Y5", "city": "Dublin", "firstname": "Rakesh", "lastname": "Jesadiya", "customer_id": null, "email": "rakesh@jesadiya.com", "telephone": "4422531111", "same_as_billing": 1 } }
Response:
[ { "carrier_code": "matrixrate", "method_code": "matrixrate_1", "carrier_title": "Select Shipping Method", "method_title": "Standard Shipping - Small", "amount": 5, "base_amount": 5, "available": true, "error_message": "", "price_excl_tax": 5, "price_incl_tax": 5 } ]
Step 5) Fetch the available Payment method for the cart.
Fetch payment method by setting shipping carrier and shipping code in the payload of shipping-information API.
Action: POST
API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId/shipping-information
(Example: https://magento.test/rest/default/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ/shipping-information)
Payload:
{ "addressInformation": { "shipping_address": { "region": "Dublin", "country_id": "IE", "street": [ "123 Oak Ave" ], "postcode": "W34 X5Y5", "city": "Dublin", "firstname": "Jane Guest", "lastname": "Doe Guest", "customer_id": null, "email": "rakesh@wearejh.com", "telephone": "4445531111" }, "billing_address": { "region": "Dublin", "country_id": "IE", "street": [ "123 Oak Ave" ], "postcode": "W34 X5Y5", "city": "Dublin", "firstname": "Jane Guest", "lastname": "Doe Guest", "customer_id": null, "email": "rakesh@wearejh.com", "telephone": "4445531111" }, "shipping_carrier_code": "matrixrate", "shipping_method_code": "matrixrate_1" } }
Response:
{ "payment_methods": [ { "code": "checkmo", "title": "Check / Money order" }, { "code": "cashondelivery", "title": "Cash On Delivery" }, { "code": "adyen_cc", "title": "Credit Card" } ], "totals": { .... } }
Step 6) Place an Order for the Guest customer.
In step 5, you can able to see different payment methods available for checkout.
Here In our case, two offline payment methods and one online payment method called adyen_cc
You have to pass the payment method code to the payment-information API.
Action: POST
API URL: <SITE_URL>/rest/<store_code>/V1/guest-carts/:cartId/payment-information
(Example: https://magento.test/rest/default/V1/guest-carts/w04QKi5YeYh6CC1tTFbpY1sPNKhbrMIZ/payment-information)
Payload:
{ "email": "guest@jesadiya.com", "paymentMethod": { "method": "cashondelivery" }, "billing_address": { "email": "rakesh@wearejh.com", "region": "County Kerry", "region_id": 0, "region_code": "", "country_id": "IE", "street": [ "123 Dublin street" ], "postcode": "W34 X4Y5", "city": "Dublin", "telephone": "441XXXXX44", "firstname": "Janes Guest", "lastname": "Does Guest" } }
Response: Order Entity id. (1200)
All the above steps are for the Guest Checkout and you will be able to place an order using REST API.
I hope this will create a guest order for Magento.
Thank you.