Support your extension for the new CoCart API

With CoCart v3 near a code freeze, I’m excited to share that the new API is more connected to the front-end of WooCommerce than before.

New matching filters are included for the cart response so you can connect any of the filters you may have already used in your WooCommerce extension to filter the cart template to do the same for CoCart.

For example, your extension may have changed how the subtotal displays in the cart using woocommerce_cart_item_subtotal.

The same can be done for CoCart using cocart_cart_item_subtotal returning the same results and using the same arguments the filter uses.

This enables your extension to be compatible no matter what the store owner decides to use. Its that simple and only one additional line of code is required.

Take WooCommerce Free Gift Coupons extension for example. All that is required for the subtotal to match via the REST API is to apply the matching filter like so.

add_filter( 'cocart_cart_item_price', array( 'WC_Free_Gift_Coupons', 'cart_item_price' ), 10, 2 );

Matching filters

WooCommerce FilterCoCart Filter
woocommerce_cart_item_namecocart_cart_item_name
woocommerce_cart_item_pricecocart_cart_item_price
woocommerce_cart_item_quantitycocart_cart_item_quantity
woocommerce_cart_item_subtotalcocart_cart_item_subtotal
All filters use the same arguments and are in the same order.

If you need any assistance, you can join me and other developers in the CoCart Community via Slack.

I look forward to seeing more WooCommerce extensions supporting CoCart.

P.S. Not all matching filters can just be placed, some results may have to return formatted a little differently depending on how you have developed your extension for that particular area.

New Add-on in Development: Carts in Session

One of the many things users have requested, (that WooCommerce don’t provide by default) is the ability to view the carts in session.

Today I am happy to share a MVP of an add-on I’m developing that will allow you to do just that.

It is currently only available on GitHub. As it is in the alpha stage, there is still work to be done but it will give you the basics to begin with.

I look forward to your feedback and requests.

Enjoy.

Sébastien.

CoCart JavaScript Library v1.0.0 Release Notes

Updated: 25th January 2021

Excited to share finally a JavaScript library to help with your headless store development. Much like the WooCommerce JavaScript Library, this one is designed so authentication is optional.

Where to get it?

You can install the JavaScript Library simply running this command in your terminal.

npm install --save @cocart/cocart-rest-api

If you wish to contribute to the project you can access the GitHub repository.

Example of Use

// import CoCartAPI from "@cocart/cocart-rest-api"; // Supports ESM
const CoCartAPI = require("@cocart/cocart-rest-api").default;
 
const CoCart = new CoCartAPI({
  url: "http://example.com"
});

You can decide to use CommonJS (CJS) or ECMAScript Modules (ESM).

Since there is only one API version at this time there is no need to define the version when setting up your API constant. All that is required is the URL of your site.

FYI

This library will NOT support the LEGACY API of CoCart.

If you are authenticating a customer then you need to set the consumerKey (a.k.a Username) and consumerSecret (a.k.a Password).

// import CoCartAPI from "@cocart/cocart-rest-api"; // Supports ESM
const CoCartAPI = require("@cocart/cocart-rest-api").default;
 
const CoCart = new CoCartAPI({
  url: "http://example.com",
  consumerKey: "sebtest123",
  consumerSecret: "happycoding24"
});

You can then proceed to use the library to make requests like so.

Get Cart

// Get Cart
CoCart.get("get-cart", {
  thumb: true, // Returns product thumbnail
})
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});

Add item to cart

// Add item to cart
CoCart.post("add-item", {
  product_id: "32",
  quantity: 1
})
.then((response) => {
  // Successful request
  console.log("Response Status:", response.status);
  console.log("Response Headers:", response.headers);
  console.log("Response Data:", response.data);
})
.catch((error) => {
  // Invalid request, for 4xx and 5xx statuses
  console.log("Response Status:", error.response.status);
  console.log("Response Headers:", error.response.headers);
  console.log("Response Data:", error.response.data);
})
.finally(() => {
  // Always executed.
});

You can view other examples provided in the documentation. Simply click on Node.js in the top right corner to see the command.

CoCart Lite v2.8.3 Release Notes

Excited to announce that CoCart Lite 2.8.3 is now publicly available! This is a minor release so everything is backwards compatible with the previous version.

As always, it is recommend to create a backup of your site before updating.

What’s new in CoCart v2.8.3?

This is the last minor update for CoCart v2 or at least for this year I think.

What’s new is that multi-site networks now properly engage with the site admin once the plugin is activated on the network.

A few tweaks have been made so we don’t add the version of the plugin per site. All admin notices now show via the network admin and if you are new to CoCart, you are redirected to the getting started page in the network admin upon first activation.

Before network sites would only see “Plugin activated” which makes the site admin feel left out.

A couple of fixes were also made.

Headers were already sent when filtering rest_pre_serve_request. This appeared to only cause issues when you have WP_DEBUG enabled when using CoCart locally.

And finally, a few errors were not returning the status error in the correct format.

That is it. I hope you are doing well and have a good x-mas. ?

Database changes

This release does not introduce any changes to the database.

Documentation

This release does not introduce any changes to the documenation.

CoCart Lite v2.8.2 Release Notes

Excited to announce that CoCart Lite 2.8.2 is now publicly available! This is a minor release so everything is backwards compatible with the previous version.

As always, it is recommend to create a backup of your site before updating.

What’s new in CoCart v2.8.2?

Two things have been done to improve the loading of a cart from session via the web. Load the chosen shipping method and cart fees.

In addition to this minor release, a request was made to be able to filter the parameter name cocart-load-cart, used for loading the cart from session via the web to provide more white labelling for their client.

You can find an example for this filter under “White Labelling”.

Database changes

This release does not introduce any changes to the database.

Documentation

All examples provided for setting a cart key have been corrected. Due to an over look on my part, I mistakenly documented the cart_key parameter as a standard data parameter and not a global parameter that requires to be queried.

Not many developers who had issues with this, saw the error and corrected this in their development but not reported the error to me.

Others had tried to report the error but did not make it clear to me as to what was wrong and moved on. Now after several releases and finally getting someone to report clearly what was wrong, I was able to correct it.

I apologise for the mistake and hope you try again with the new examples provided. I have also added a warning notice in the documentation to remind you that it is not to be passed as a data parameter.