WC_Data, Data Storage & Manipulation


A few key concepts are at the base of all WC_Data objects. Understanding how the class getsIn “getter” methods, manipulatesWith “setter” methods and stores dataWith “update” methods can provide insights into how you can set various classes to interact with custom data involved in your project. In this section we’ll look at:


WC_Data Properties

List and Description of WC_Data Properties

data vs extra_data

      WC_Order_Item $data


      WC_Order_Item $extra_data


The $data property is used to hold information that is fundamental to the representation of the class family. The $extra_data propertyPrimarily used by the product, order_item, and payment token families is used by descendants of the family to add data without overwriting the $data settings.

data vs extra_data vs meta_data

  • While data is the core information, and extra data is the inheritance information, meta_data is the location for storing completely non-standard, custom information.
  • In most cases, the specific table utilized for storing metadata is defined by the data store’s $meta_type property. This is a feature implemented by the WC_Data_Store_WP class
  • $meta_data utilizes the WC_Meta_Data class to monitor changes to this property, in order to minimize unnecessary database writes.

The WC_Meta_Data class helps ensure that WC_Data’s save_meta_data() method does not perform any unnecessary database writes.

WC_Data methods

Comprehensive list of public and protected WC_Data methods
__construct __sleep __wakeup __clone get_data_store
delete save __toString get_data get_data_keys
filter_null_meta* get_meta_data is_internal_meta_key* get_meta meta_exists
add_meta_data update_meta_data delete_meta_data delete_meta_data_by_mid maybe_read_meta_data*
save_meta_data set_id set_defaults set_object_read get_object_read
set_prop* get_changes apply_changes get_hook_prefix get_prop*
error* get_id set_date_prop* set_props read_meta_data
read_meta_data set_meta_data get_extra_data_keys

* method is protected (only available when writing methods within descendants of WC_Data)

Special methods

WooCommerce makes extensive use of magic methods to ensure simple and consistent behavior for WC_Data objects

Database operation methods

  • delete()
  • save()
  • read_meta_data( $force_read = false )
  • save_meta_data()
  • maybe_read_meta_data() WooCommerce uses this to prevent unnecessary database reads. $data->read_meta_data(true) will force fresh reads.
The WC_Data object minimally interacts with the database and only does so through its $data_store


  • get_data_store()
  • get_id()
  • get_data()
  • get_data_keys()
  • get_extra_data_keys()
  • get_meta_data()
  • get_meta( $key = ”, $single = true, $context = ‘view’ )
  • get_object_read()
  • get_changes()
  • get_hook_prefix()Used to determine an object’s hooks/filters
  • get_prop( $prop, $context = ‘view’ )This is the core method for most getters
  • set_date_prop( $prop, $value )
Most useful information for a WC_Data object is held in protected properties. Therefore, use of getters is required to access object specific information


  • set_meta_data( $data )
  • add_meta_data( $key, $value, $unique = false )
  • update_meta_data( $key, $value, $meta_id = 0 )
  • delete_meta_data( $key )
  • delete_meta_data_by_mid( $mid )
  • delete_meta_data( $key )
  • delete_meta_data_by_mid( $mid )
  • set_defaults()
  • set_object_read( $read = true )
  • set_props( $props, $context = ‘set’ )Used by data stores to load class
  • set_prop( $prop, $value )Core method for most setters
  • apply_changes()Does NOT automatically result in a database write
Setter methods update information in the WC_Data object held in working memory. However, one of the Database Operations Methods must be called to make the change in the database

Checkers (Boolean)

  • filter_null_meta( $meta )
  • is_internal_meta_key ( $key )
  • meta_exists( $key = ” )
Because $meta_data could be stored in different tables, depending on the site’s configured datastores, built in boolean methods should be used to determine meta_data’s status.


Now that you understand the difference between the different data types, and understand the methods at the core of WC_Data functionality, you can start to see the opportunities for easy customization.

In the next topic, we’ll look at how Data Stores are used provide a flexible method of database storage for WC_Data objects

But first, check your knowledge.

Knowledge Check

Hint: Be sure to look in blue boxes and at purple text “tooltips” for notes while you take the quiz.


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

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