Overriding a plugin's pluggable function in theme's function
-
13-04-2021 - |
سؤال
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()
.