Tutorial: Disable Load Cart from Session

As the only feature in CoCart that does not use the REST API, some developers requested that they have the option to disable it.

A new filter cocart_disable_load_cart was introduced so that you can do just that.

add_filter( 'cocart_disable_load_cart', function() { return true; });

Once the filter is set to true. “Load Cart from Session” will no longer be available.

6 things you can do with CoCart Products

If you have previously tried WooCommerce REST-API to retrieve products, you will have found that it has a few limitations and some parameters don’t provide options designed for frontend purposes.

Order By

One of the top things that is important when retrieving products is the order.

Here are the possible options you can order products by that the WooCommerce REST-API does not offer.

  • alphabetical – Products return alphabetical
  • reverse_alpha – Products return alphabetical in reverse
  • by_stock – Products return in order by stock amount
  • review_count – Products return in order of review count
  • on_sale_first – Products return in order of products on sale first
  • featured_first – Products return in order of featured first
  • price_asc – Products return in order of price in ascending order
  • price_desc – Products return in order of price in descending order
  • sales – Products return in order of total sales
  • rating – Products return in order of average review rating
curl -X GET https://wp-demo.cocart.xyz/wp-json/cocart/v1/products?orderby=price_desc \
  -H "Content-Type: application/json" \

Show Reviews

One cool thing you can do is retrieve the product reviews along with the product details for each product or an individual product without requesting them later.

Set show_reviews as true as a parameter in your request.

curl -X GET https://wp-demo.cocart.xyz/wp-json/cocart/v1/products?show_reviews=true \
  -H "Content-Type: application/json" \

Customer’s email address and location details are not returned.

Return all Variations

This particular option was requested by many but is also one that can produce a slow query response depending on how it is used and how many variations a particular product has.

Set return_variations as true as a parameter in your request and whether your returning many products or a single variable product, all variations and it’s details will return.

curl -X GET https://wp-demo.cocart.xyz/wp-json/cocart/v1/products?return_variations=true \
  -H "Content-Type: application/json" \

Get the right image size

In the WooCommerce REST-API, products only return the original image size uploaded for the product. That can be too much bandwidth depending on the filesize of the image.

With CoCart ¨Products, you can get the image size of your choosing for the featured product image or any in the product gallery. All WordPress image sizes including the custom sizes WooCommerce create are returned and any custom image sizes that you register personally.

All image sizes return correctly if that particular size has the image resized. If the image size was registered after the image was uploaded then you will have to use regenerate thumbnails plugin or other of your choosing in order for the size to return correctly.

Here are the sizes available:

  • thumbnail
  • medium
  • medium_large
  • large
  • woocommerce_thumbnail
  • woocommerce_single
  • woocommerce_gallery_thumbnail
  • shop_catalog
  • shop_single
  • shop_thumbnail
  • full

Filter by Category or Tag

While this not new, the difference in CoCart Products REST-API is that you filter products by the category or tag slug instead of the ID, making it easier to remember what to filter the products by… for example “tshirts” instead “25”.

curl -X GET https://wp-demo.cocart.xyz/wp-json/cocart/v1/products?category=tshirts \
  -H "Content-Type: application/json" \

Submit Product Review

WooCommerce REST-API provides the same only it allows administrator’s to submit. CoCart Products allows customers to submit a review. This is the only feature that requires authentication.

curl -X POST https://wp-demo.cocart.xyz/wp-json/cocart/v1/products/reviews \
    -u username:password \
    -H "Content-Type: application/json" \
    -d '{
      "product_id": 329,
      "review": "Naughty Dog has done it again. This game is sick. I look forward to playing this over and over again like I did the first one. 5 stars does not do it justice. Game of the year for sure.",
      "reviewer": "John Doe",
      "reviewer_email": "john.doe@example.com",
      "rating": 5

Want a feature for CoCart Products? Send in your request.

Adding products to the cart

Outdated Article

Please view the CoCart documentation for more accurate and up to date guide.

Want to know how to add products to the cart with CoCart? Checkout these examples below.

Simple Product

Simple products are exactly that. Just the product ID and the quantity are required.

Variable Product

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.

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.