I came across this snippet - https://gist.github.com/mikejolley/1751128 - that creates a function with the same name as an existing WC function as a means of overriding it.

I also learnt that this way of overriding works because the original function is 'pluggable' due to the !function_exists() function.

My question is since plugins gets loaded before themes i.e., WC would have created the function first, shouldn't the attempt to override the function fail because the 'if (!function_exists('woocommerce_template_loop_add_to_cart'))' condition isn't satisfied?

Appreciate any input!


The WooCommerce file that defines its pluggable functions isn't loaded until after themes are loaded. It does this by hooking a function that includes the file into the after_setup_theme hook:

add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 );

That's from the /includes/class-woocommerce.php file in WooCommerce. The include_template_functions() function includes the file that defines functions like woocommerce_template_loop_add_to_cart().


You are correct, plugins are loaded before the theme is, therefore you generally can't override a pluggable function in a plugin with a theme.

However, you can override the function with another plugin that loads before the plugin does. An MU plugin will load before any regular plugin (such as WooCommerce) does, and then plugins are loaded in alphabetical order. So a plugin named "My custom WooCommerce overrides" will load before WooCommerce does, allowing you to override the pluggable functions.

In this particular instance with this snippet though, the function woocommerce_template_loop_add_to_cart may not be loaded until the template parts are being pulled in, so the theme's functions.php will in fact have loaded first

  • This answer is incorrect. The entire point of pluggable functions (even within another plugin, as opposed to WP's pluggable functions) is that you CAN override them - otherwise, they're not "pluggable." Plugins that have their own pluggable functions will load those functions later (at least if they have been built correctly). Usually, this means that pluggable functions in the plugin will not be loaded until at least after_setup_theme, thus allowing you to override them in functions.php or with another plugin. – butlerblog May 16 '19 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.