How do I determine the values to use in the payload for the add to cart method in the Commerce API

drupal.stackexchange https://drupal.stackexchange.com/questions/298648

  •  01-03-2021
  •  | 
  •  

Question

The documentation for the Commerce API gives the example payload below for adding items to a cart. How do I find the type and ID values for the product variants in my store configuration?

{
  "data": [
    {
      "type": "product-variations--default",
      "id": "e8daecd7-6444-4d9a-9bd1-84dc5466dba7",
      "meta": {
        "quantity": 1
      }
    }
  ]
}
Was it helpful?

Solution

I eventually located these using the path https://mydomain.com/jsonapi/product-variations/default.

A call to https://mydomain.com/jsonapi exposes the end-points you can use, based on your product configuration. Mine looked like this:

        "product--default": {
            "href": "http:\/\/mydomain.com\/jsonapi\/products\/default"
        },
        "product-attribute": {
            "href": "http:\/\/mydomain.com\/jsonapi\/product-attributes"
        },
        "product-attribute-value--rate": {
            "href": "http:\/\/mydomain.com\/jsonapi\/product-attribute-values\/rate"
        },
        "product-type": {
            "href": "http:\/\/mydomain.com\/jsonapi\/product-types"
        },
        "product-variation--default": {
            "href": "http:\/\/mydomain.com\/jsonapi\/product-variations\/default"
        },
        "product-variation-type": {
            "href": "http:\/\/mydomain.com\/jsonapi\/product-variation-types"
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top