WC_Data Filters and Actions

Overview

The inheritance pattern used by the WC_Data families enables a standard pattern for the action and filter hooks available to developers making it easy to quickly modify behavior across WooCommerce. Once you’re aware of the standard hooks that you can expect to be available to you, you can skip the process of searching through the documentation and codebase, Unless you like spending hours with search engines, Stack Exchange, and IDE searches and insert your custom code using the standard patterns.

 

 

Let’s take a look at what that entails…….

Data Object Hook Prefixes

  • Each WC_Data object has a hook prefix, used for it’s specific ‘getter’ method filters
  • The hook prefix is accessed by the get_hook_prefix() protected method
  • By convention the hook prefix is equal to “woocommerce_{$object_type}_get”

Contexts and hooks

  • Throughout the WC_Data class get and set methods, an optional $context variable can be passed
  • $context is used to determine if specific filters should be applied.
  • In the case of getters the default value, ‘view’, applies the filter. While the alternate value ‘edit’ returns raw data. The ‘edit’ context is typically used for fields in the WP Admin dashboard
  • In the case of setters, the default value is usually ‘set’, with no specific functionality assigned to that value…yet. I’m not sure what the plans are for this in the long run–but will update if I find anything out ¯\_(ツ)_/¯

Pre and Post DB save actions

WooCommerce also provides action hooks before and after a database save, with access to the object$this, and it’s data store$this->data_store

Family specific hooks and filters

  • Beyond the standard WC_Data implemented actions and filters, each family also implements its own series of hooks and filters
  • These are all covered in the docs covering all of these would be excessive however there are some patterns we can observe:

Boolean filters

  • Each boolean method (e.g. is_purchasable()) is typically passed through a filter

Getter filters

  • Each getter method eventually calls either the get_prop or get_meta method, each of which is passed through a filter. … as long as the ‘view’ context is used

Create filters

  • Default values are typically passed through a filter before a new data object is created This is found in the data store create method.

Actions in WC_Data

  • Action hooks are spread more unevenly than filters, but generally they are most frequently seen in objects directly involved in the checkout processsuch as the Order and Order_Item Families

Conclusion

This concludes our outline of the WC_Data approach to action and filter hooks. You should now be able to confidently navigate to filters and actions that will allow you to extensively customize the behavior of WooCommerce, without breaking into “heavy coding”

 

In the next section, we’ll look at another method to achieve even further customization in WooCommerce through a more robust method: extending the WC_Data classes

But let make sure you’ve got the core concepts down first.

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