Magento 2 How to create cart ready for checkout in GraphQL

In this topic, we will show you how to use the cart query to retrieve information about a particular cart. Cart functionality is defined in the Quote module. A Quote represents the contents of a customer’s shopping cart. It is responsible for performing tasks such as: - Tracking each item in the cart, including the quantity and base cost - Determining estimated shipping costs - Calculating subtotals, computing additional costs, applying coupons, and determining the payment method

Syntax

Copy
{cart(cart_id: String!) {Cart}}

The following query shows the status of a cart that is ready to be converted into an order.

Request

Copy
{
  cart(cart_id: "CKqz0w9cbZLhWSKHXk4m5Tz2jOFCbJkv") {
    email
    billing_address {
      city
      region {
        code
        label
      }
      country {
        code
        label
      }
      firstname
      lastname
      postcode
      street
      telephone
      company
    }
    shipping_addresses {
      firstname
      lastname
      street
      city
      region {
        code
        label
      }
      country {
        code
        label
      }
      telephone
      company
      available_shipping_methods {
        amount {
          currency
          value
        }
        available
        carrier_code
        carrier_title
        error_message
        method_code
        method_title
        price_excl_tax {
          value
          currency
        }
        price_incl_tax {
          value
          currency
        }
      }
      selected_shipping_method {
        amount {
          value
          currency
        }
        carrier_code
        carrier_title
        method_code
        method_title
      }
    }
    items {
      id
      product {
        name
        sku
      }
      quantity
    }
    available_payment_methods {
      code
      title
    }
    selected_payment_method {
      code
      title
    }
    applied_coupons {
      code
    }
    prices {
      grand_total {
        value
        currency
      }
    }
  }
}

Response

Copy
{
  "data": {
    "cart": {
      "email": null,
      "billing_address": null,
      "shipping_addresses": [],
      "items": [
        {
          "id": "5",
          "product": {
            "name": "Sparta Gym Tank",
            "sku": "MT08"
          },
          "quantity": 1
        }
      ],
      "available_payment_methods": [
        {
          "code": "checkmo",
          "title": "Check / Money order"
        }
      ],
      "selected_payment_method": {
        "code": "",
        "title": ""
      },
      "applied_coupons": null,
      "prices": {
        "grand_total": {
          "value": 29,
          "currency": "USD"
        }
      }
    }
  }
}

That's it!!

If you are looking for more GraphQL solutions then check out our another article which is List of GraphQL Queries and Mutations for Magento 2.

I hope this article helps you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.




Search
Recent Posts
    Tags
    Newsletter
    SocialFollow on Twitter
    Visit now!