Website Building » WooCommerce » How Do I Put an Item in Cart Through REST API Provided by WooCommerce?

How Do I Put an Item in Cart Through REST API Provided by WooCommerce?

Last updated on January 17, 2023 @ 11:30 am

In order to put an item in the cart through the WooCommerce REST API, you will need to make a POST request to the /cart/items endpoint. The body of your request should include the following parameters:

product_id: The id of the product you want to add to the cart

quantity: The number of items you want to add to the cart

You can also optionally include the following parameters:

variation_id: If the product you are adding to the cart is a variation of a product, you will need to include the variation id

variations: If the product you are adding to the cart is a variable product, you will need to include an object containing all of the variation attributes that have been selected. Each attribute should be included as an object with a “key” and a “value”.

For example, if you are adding a variable product to the cart that has two attributes (color and size), your variations parameter would look like this:

PRO TIP: If you are using the WooCommerce REST API to put an item in your cart, be aware that there is a known issue with the API which can cause your items to be duplicated in the cart. This issue has been fixed in the latest version of WooCommerce, but if you are using an older version, be sure to check your cart carefully before proceeding to checkout.
{  
   "variations":{  
      "color":"blue",
      "size":"medium"
   }
}

Conclusion:

Therefore, in order to add an item into shopping cart provided by WooCommerce through REST API, we make a POST request to their given endpoint which is ‘/cart/items’ . Following this, we provide required (and optional) parameters in JSON format such as product_id, quantity, variation_id, variations (etc.) which helps us successfully add an item(s) in shopping cart provided by WooCommerce.

Drew Clemente

Drew Clemente

Devops & Sysadmin engineer. I basically build infrastructure online.