Product page

How to display price and availability on a product page

The Problem

You've created a product page with your favorite CMS and now you want to display the price and the availability of the selected product. You also need to implement the "add to cart" function on the related button.

Solution

To retrieve the price and availability of an SKU, send a GET request to the /api/skus/:id endpoint and include the associated prices.

Example

The following request retrieves the price and the availability of the SKU identified by the "aBmNkPQRst" ID:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus/aBmNkPQRst?include=prices' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

For performance reasons, the inventory information is only returned when fetching a single SKU.

To implement the "add to cart" function, please refer to this guide:

pageAdding products to cart

Mapping

The image below shows the three main dynamic elements of the page (selling price, full price and inventory information) and how each of these are mapped to a specific field of the SKU or price objects.

Additional notes

Getting the SKU ID from its code

If you have the SKU code and you want to retrieve the related ID, send a GET request to the /api/skus endpoint and filter it by code, taking care to request the ID field only.

The following request retrieves the ID of the SKU identified by the code "TSHIRTB5B5B5XL":

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus?filter[q][code_eq]=TSHIRTB5B5B5XL&fields[skus]=id' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

Getting a list of SKU prices

If you need to display the prices associated with a list of SKUs (with no inventory information) send a GET request to the /api/skus endpoint, filter it by code, and include the SKU prices.

The following request retrieves the prices of the SKU identified by the "TSHIRTB5B5B5XL" code:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/skus?filter[q][code_eq]=TSHIRTB5B5B5XL&include=prices' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

More to read

See our documentation if you need more information on how to filter data, include associations, and manage sparse fieldsets or if you want to have an overview of all the specific permissions and restrictions set for a sales channel API client.

Last updated