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.
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.