Want to know how to add products to the cart with CoCart? Checkout these examples below.
Simple products are exactly that. Just the product ID and the quantity are required.
Notice for a variable product you pass along the attribute the customer selected too. Variable products may have more than one attribute and some variations maybe set to allow attributes to be open to any option so passing along the attribute the customer selected is always best with multiple options.
When adding the attribute make sure you start the key with attribute_pa_ followed by the attribute slug of the option selected and then the value.
The best and only REST API for the frontend of WooCommerce. Build your headless store today and join a fast growing community of developers to help you out.
Before now, WooCommerce authentication method was left open so any 3rd party REST API could use there authentication method. This gave no limits for users to authenticate with CoCart.
However, due to the added restriction for just their REST API, (which could change in the future as WordPress will provide a more generic version) CoCart could no longer authenticate the WooCommerce way on either version of CoCart’s API.
But have no fear as today version 2.0.5 provides a small update to re-enable WooCommerce’s authentication method again for CoCart.
CoCart users can now authenticate the #WooCommerce way again. ?
On top of the update, I’m going to show you how to authenticate the WooCommerce way.
The guide below is assuming you are making requests with HTTPS.
First if you haven’t already created any keys you will need to go the advanced section in your WooCommerce settings.
From there you need to press Add key button. Then select the user who will be using the key. Set the permissions to READ/WRITE so all of the CoCart API is accessible. Enter a description if you wish and then press Generate API key button.
You will then be presented with your consumer key and secret which you will need to copy before moving away from the page as warned to the user in the screenshot below.
Once you have your key created you can then make any authenticated request with CoCart like so.
If you would like to understand more about authenticating with WooCommerce read the authentication section in their REST API documentation.
With CoCart v2 it is now possible to filter the responses before they are returned. In this small tutorial I will show you how you can change the response when getting the cart contents.
What you see when you return the cart contents is how WooCommerce formats the data. This format is what WooCommerce or a WooCommerce extension use to be able to identify the item in cart should they need to alter it before or after being added to the cart or even update it.
However, via the REST API this particular key is not a requirement to have as the parent of the array of item data and since we are not altering the core of WooCommerce, we can remove it.
Some of you who are developing for Android have requested to remove the cart item key as the parent array in order to make it easier to capture the list of items.
It’s very simple and easy to do. You will however, require to have some knowledge of PHP in order to apply the filter.
Here is what you need to do. Copy the source and apply it either to your child-theme’s functions.php file or use a plugin like My Custom Functions to apply the filter.