Implementing Custom Data Stores

Overview

It’s no overstatement to say that the addition of the data store layer of abstraction in WooCommerce 3 opened up an entirely new world of possibilities. Whether your objective is customizing behavior, optimizing performance, or creating an entirely new sales proposition in WooCommerce, data store can provide an elegant way to accomplish the task.

This level of coding can be considered “heavy lifting” in any WP/WooCommerce group of developers. While the considerations are significant, you’ll see that the process is fairly straightforward, with several major players in the industry providing us with a template for success

Custom Data Stores: A Word of Caution

It is generally known: Historically, WooCommerce has not been highly performant software. Especially with larger stores with over 10k products/purchase orders

As a result of this, the innovative community of WordPress and WooCommerce developers is toiling away to solve this problem. The most notable efforts are Liquid Web’s flattened order table plugin and the custom product table plugin that have been developed by WooCommerce

The big players are working on developing solutions, and there’s a good chance that their solutions will end up in the Woo Core at some point in the future. So make sure you’re not creating extra work for yourself down the line by reinventing the wheel.

And with that…

Custom data stores:
Your key to doing whatever you want!

  • All data stores are loaded by the WC_Data_Store:load method, which is run through two filters
  • This means that every WC_Data class can be set up with your custom data store. So, not only can you customize the way data is stored in the database—you can set unique property and meta values, which will drive custom behavior–without the need to extend the WC_Data class itself!
class-wc-data-store.php constructor

Requirements

  • In creating a custom data store the key requirement is to implement the WC_Object_Data_Store_Interface. Otherwise, WC_Data_Store will throw an error.There is an instanceof check in WC_Data_Store’s constructor
  • There are also several ‘assistant’ interfaces, which are not mandatory (i.e. will not result in an error). However, they help ensure reliable behavior.
Methods required by the WC_Object_Data_Store interface

Building your custom data store class

Converting to the new data store

Once you’ve created your new custom data store class, and applied it using either the ‘woocommerce_data_stores’ or the ‘woocommerce_{$object_type}_data_store’ filter. You’ll likely need to convert existing data over. This is the most complex part of the task. We’ll use Liquid Web’s (LW) plugin as an example.

  1. You’ll need a method, for retrieving the data from the database using the old method. LW’s populate_order_from_post_meta is a good example
  2. Next, you’ll need to convert the data into the new format using your data object’s save() and/or update_post_meta() method. LW’s plugin achieves this in the populate_from_meta method on line 408.
  3. Finally, scrub the old data from your database (line 418)

Note: if your are planning on distributing your plugin, you need to include a user friendly method for both migrating to and from your custom data stores. If this is your situation LW’s WP-CLI methodology is a great methodology to examine.

Conclusion

Complex but powerful, custom data stores provides an extraordinary amount of flexibility in customizing WooCommerce.

While it is a bit daunting, you should now have a clear idea of the steps involved in creating a custom data store to support your project. If you need more specifics, the resources provided in the references section are excellent. and even includes an explainer from WooCommerce in their Wiki

Take a deep breath, create a backup, and test your mettle in unlocking the full potential of WooCommerce for your project.

This conclude the topic and the course! Remember this course is designed to be searchable, nimble, and is continuously updated, so keep this material in your back pocket and use it to breeze through your next WooCommerce project!

Knowledge Check

×

Keyboard shortcuts

CTRL+Shift+F Search slideshow
F Fullscreen view
CTRL+Click Zoom in
Esc Topic overview
Right arrow,
Down arrow
Next slide
Left arrow,
Up arrow
Previous slide

Color codes

Tooltip
Hover over text more additional info
Link to an external resource
Link to an internal slide
If buttons aren't working, click in the screen to "focus" your browser
RSS
LinkedIn
Share