When a product is added to the customer’s shopping cart, Magento produces a quote and the quote will be used to perform the following tasks:
Step1 – Create quoteId
Step2 – Create cartItems
Step3 – Create shipping information & billing information
Step4 – Create payment methods
But before create a quoteId we need to create a customer Authorization Token for loggedin user for this we need to do the following –
The customer’s authorization token must be contained in the authorization header to create a cart.
I will use Postman to interact with Magento 2 API and create a quote. First you have to generate a customer access token with this endpoint.
Request Type : POST
1 |
Endpoints: <host_name>/rest/<store_code>/V1/integration/customer/token |
like this – http://local.magento.com/rest/V1/integration/customer/token
customer token is – ugihamdexb2jwazgwebn8svne4340c22
Step1 – Create quoteId
Request Type – POST
1 |
Endpoints - <host>/rest/<store_code>/V1/carts/mine |
like as : http://local.magento.com/rest/default/V1/carts/mine
Payload – None
1 2 |
Authorization Bearer <customer token> (ugihamdexb2jwazgwebn8svne4340c22) |
Response – 12
Step2 – Create cartItems
Request Type – POST
EndPoinst –
1 |
<host>/rest/<store_code>/V1/carts/mine/items |
like this – (http://local.magento.com/rest/default/V1/carts/mine/items)
1 2 3 4 5 6 7 8 |
Payload - { "cartItem": { "sku": "24-MB01", "qty": 1, "quote_id": "13" } } |
Customer_token – ugihamdexb2jwazgwebn8svne4340c22
Headers:
Content-Type application/json
1 2 3 4 5 6 7 8 9 10 |
Response - { "item_id": 9, "sku": "24-MB01", "qty": 1, "price": 0, "product_type": "simple", "quote_id": "14" } |
Step3 – Create shipping information & billing information
Request Type –POST
EndPoinsts –
1 |
<host>/V1/carts/mine/shipping-information |
(http://local.magento.com/rest/default/V1/carts/mine/shipping-information)
Authorization: Bearer token – customer token
Header –
Content-Type application/json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Payload - { "addressInformation": { "shipping_address": { "region": "Gujarat", "region_id": 580, "region_code": "GJ", "country_id": "IN", "street": [ "Shiv Elite" ], "postcode": "364001", "city": "Bhavnagar", "firstname": "Manjeet", "lastname": "maurya", "email": "manjeet@gmail.com", "telephone": "8141102201" }, "billing_address": { "region": "Gujarat", "region_id": 580, "region_code": "GJ", "country_id": "IN", "street": [ "Shiv Elite" ], "postcode": "364001", "city": "Bhavnagar", "firstname": "Manjeet", "lastname": "Maurya", "email": "manjeet@gmail.com", "telephone": "8141102201" }, "shipping_carrier_code": "flatrate", "shipping_method_code": "flatrate" } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
Response: { "payment_methods": [ { "code": "checkmo", "title": "Check / Money order" } ], "totals": { "grand_total": 5, "base_grand_total": 5, "subtotal": 0, "base_subtotal": 0, "discount_amount": 0, "base_discount_amount": 0, "subtotal_with_discount": 0, "base_subtotal_with_discount": 0, "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": 0, "shipping_incl_tax": 5, "base_shipping_incl_tax": 5, "base_currency_code": "USD", "quote_currency_code": "USD", "items_qty": 1, "items": [ { "item_id": 9, "price": 0, "base_price": 0, "qty": 1, "row_total": 0, "base_row_total": 0, "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": 0, "base_price_incl_tax": 0, "row_total_incl_tax": 0, "base_row_total_incl_tax": 0, "options": "[]", "weee_tax_applied_amount": null, "weee_tax_applied": null } ], "total_segments": [ { "code": "subtotal", "title": "Subtotal", "value": 0 }, { "code": "shipping", "title": "Shipping & Handling (Flat Rate - Fixed)", "value": 5 }, { "code": "tax", "title": "Tax", "value": 0, "extension_attributes": { "tax_grandtotal_details": [] } }, { "code": "grand_total", "title": "Grand Total", "value": 5, "area": "footer" } ] } } |
Step4 – Create payment methods
Endpoinsts –
1 |
<host>/rest/V1/carts/mine/payment-information |
(http://local.magento.com/rest/default/V1/carts/mine/payment-information)
Request-Type – POST
Authorization: Bearer token – customer token
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Payload: { "paymentMethod": { "method": "checkmo" }, "billing_address": { "email": "manjeet@gmail.com", "region": "Gujarat", "region_id": 580, "region_code": "GJ", "country_id": "IN", "street": ["Shiv Elite"], "postcode": "364001", "city": "Bhavnagar", "telephone": "8141102201", "firstname": "Jignesh", "lastname": "Parmar" } } |
Response: “6”
Order Created Successfully.
bluethinkinc_blog
2023-02-08