How to Create a Quote by Calling API in Magento 2

When a product is added to the customer’s shopping cart for the first time, a quote will be created by Magento. And the quote will be used to perform the flowing tasks:

  • Track items which the customer wants to purchase, including their quantity and base cost
  • Collect customer’s information: billing and shipping addresses
  • Calculate shipping costs
  • Calculate the order subtotal, add other costs such as shipping fees, taxes, etc. and apply coupons to decide the grand total
  • Decide the payment method
  • Place the order for the merchant to fulfill

It’s necessary for store owners to get the quote information before further order processing. You can use REST API to get quote information in Magento 2. It is used to fetch data by sending requests and receiving responses.

In today’s article, I will provide you two steps to help you create a quote by calling API.

Let’s start!

2 Steps to create a Quote

Step 1: Create cart for logged-in customer

Firstly, let’s go through three types of users who can create a shopping cart:

  • An admin user: this users’ type has the ability to create a cart on behalf of a customer. In order for the admin requests to be created, an admin authorization token has to be provided in the call’s authorization header.
  • A logged-in customer: The customer’s authorization token must be contained in the authorization header to create a cart and add items.
  • A guest user: These users could be customers who haven’t logged in yet, or have no intention of creating an account. An anonymous user’s cart is also known as a guest cart.

Now we are going to manage the cart of a logged-in customer. All calls have to specify the token of customer q0u66k8h42yaevtchv09uyy3y9gaj2ap in the header, if otherwise not noted.

Endpoint

POST <host>/rest/<store_code>/V1/carts/mine

Headers

Content-Type application/json

Authorization Bearer <customer token>

Payload

None

Response

The quoteId: 4 is the response.

Note: This parameter is referred to as the cartId by some calls.

Step 2: Verify this step

As the quoteId values will not be displayed on the website or in Admin, there are no additional verification steps needed.

Conclusion

Above I have just shown you how to call API to create a quote. I hope this post is helpful for you. In case you have any questions or new ideas, feel free to leave a comment below.


SVG

One Step Checkout for Magento 2

Cut down 80% of checkout time & increase 30% of conversion rates (Hyva ready)

Check it out!


Image Description
Sam is the CEO & co-founder of Mageplaza, a company established to support Magento merchants with different powerful tools and resources. Sam Nguyen is also the CEO & founder of Avada Commerce, an e-commerce solution provider headquartered in Singapore – aiming to support more than a million online businesses to grow and develop.
x