Authenticating with WooCommerce: Here’s how you can do it

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.

Please Note

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.

Fetching the cart while authenticating

If you would like to understand more about authenticating with WooCommerce read the authentication section in their REST API documentation.

Happy coding!

Removing the Cart Item Key

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.

Get Cart Contents Response Example
Example of the default response you get when getting the cart contents.

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.

Image of the filtered code to remove parent item keys.

Source: https://gist.github.com/seb86/9461d6fdad9367246ad9fb87d52a9891

Once you have applied the filter, any time you get the cart contents again the results of the response will look like so.

Hope it helps with your development. If you would like more tutorials for CoCart, please let me know in the comments below.