A new beta of CoCart v2.1.0 is now available for testing with new and improved support for guest customers by saving the carts in session to a new database table for storage, performance and ease of cleanup.
Storing the carts
WooCommerce has a similar method of storing the cart data but it is only for logged in users and is stored under the
usermeta table. While this is fine, it does not help those who are guest customers neither does the cart data expire unless the cart is emptied, an order has been made or all session data has been cleared via the tools section of the WooCommerce System Status.
In the first beta I simply saved the cart to the WordPress options table
I realized that this was not the best solution should the cookie be removed on the users device, the cart would still be saved in the database which would eventually lead to increasing the size of the database due to human error.
The data not only needed to be stored but identified should the cart not have had it’s data updated or used for some time. This lead to creating a sessions table of my own
wp_cocart_carts where the cart data per customer would be stored for a period of time.
When is the cart stored?
The data of the cart is stored the moment a customer adds the first item to the cart. The cart data is then updated when another item is added or an item is updated or removed. It’s also updated when a coupon is applied, coupon removed, fees and totals are calculated.
Cleaning the carts
With the large amounts of customers successful eCommerce stores get, maintaining the carts stored in the database needs to be kept in check and not overload the size of the database.
To achieve this, the carts have to expire after X amount of time. 48 hours should be enough but it can be overridden should the store owner choose to increase or decrease the time using a filter
A cron-job is scheduled to run twice daily to clean any carts that have expired to keep the new table optimized.
For developers, I have added a new tool option to clear all carts saved in the new table via WooCommerce System Status. I recommend only using this for debugging purposes on a local environment as it does not clear the cookies stored on user devices.
Getting the carts
Once the customer has added at least one item to the cart, a cookie is created with a unique key for the cart. You then access that cookie to get the value of the cart key.
After getting the cart key you can then request to return the carts content for that cart like so.
There is no permission or authentication required to access the saved cart. As long as the cart key matches or the cart has not expired, you will be able to return the cart contents.
If you have already tried beta 1, it’s important to know that the cookie name has changed slightly to keep everything consistent throughout the code base.
In beta 1 the cookie name is called
cocart_cart_id. In beta 2 the cookie name has changed to
Wither you use CoCart for the REST-API or not, this new supported feature could be used to determine if a cart has been abandoned. This could be helpful to you with detailed information on what your customers are adding to their carts.
By default, storing the cart data was designed for guest customers only but it can also be used for registered customers too simply by filtering in the users ID as the unique key using
Pretty sweat right!
Feedback is Required
For this new release I require you (specifically who are developing for Android) to test and provide me feedback. Getting the support right for you is important.
I’m not an Android developer nor do I have the time or tools to learn Android development so I urge you to tell me what works, what does not and what is missing so I can get it right for you.
No release date is set so there is plenty of time to test.
Thank you for reading and happy testing.
Just a Warning
A new database table will be created upon activation of the plugin for the carts to be stored. Please only install the beta on a local environment.