WC_Data Overview

Overview

Let’s start with a quick overview of the role of WC_Data in WooCommerce, how it works, and why a strong grounding in the topic is essential to succeeding on large WooCommerce projects. We’ll cover:

Why Learn?

  • It is a foundational class for the majority of persistent data in WooCommerce.
  • Understanding WC_Data, and its descendants, unlocks the possibilities for customization and simplifies complexity
  • Without a strong grasp of WC_Data, you will likely fall back to coding the “WP way” instead of the “Woo way.” Your code will still work,
    but it will be brittle to failure from updates A classic example of this is the difference between using wp_get_posts() and wc_get_products()

What is WC_Data

WC_Data is an abstract class in WooCommerce that provides the foundation for:

  • Database agnostic getting and setting of properties
  • Managing database CRUD interactions through a data store
  • Managing an object’s metadata
  • Default property values
  • Tracking property changes before save
  • Providing standardized action and filter hook names
  • Converting data to JSON format
  • Throwing Exceptions when invalid data is found

Where is WC_Data used

WC_Data is used as the parent class anywhere there are a significant amount of database transactions, with persistent data, in WooCommerce

WC_Data class structure

Data Stores

  • Data Stores are the mechanism used by WC_Data to abstract the database interactions.
  • Data stores are loaded by WC_Data’s child classes in the __construct method, using the WC_Data_Store::load() static method The static load method directly calls the data store constructor to return a data store
Example of data store loaded in WC_Product constructor

Customization

WooCommerce affords 3 primary techniques for customizing WC_Data descendant behavior:

  1. Through action and filter hooks. There used to be a comprehensive list of these is available in the documentation, but that’s changed quite a bit. Fortunately, we’ve got a WC_Data specific overview here.
  2. By extending a WC_Data descendant class
  3. By creating and loading custom data stores

Each of these techniques is covered in detail, in their own topic, later in the lesson

Conclusion

This concludes the general overview of WC_Data. Now you should have a clear idea of how integral this class is to the functioning of WooCommerce, and can see how it’s core functionality provides the basis for robust customization

In the next topic, we’ll look at the fundamentals of data and data manipulation in WC_Data

But first, check your knowledge.

Knowledge Check

Hint: You can still look at the topic contents with the menu on the left hand side of the screen.

×

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