Why is it the preferred choice, to have WooCommerce function modifications placed inside the theme's 'function.php' file? Overtime, this would make the file rather large. I am of the understanding that it is generally better practice to have lots of well organised smaller files, rather than fewer much larger files.
With this in mind, what is wrong with copying the 'wc-template-functions.php' and 'wc-templates-hooks.php' files into your theme (whilst keeping its file hierarchy) and modifying these files accordingly?
As a side request, from a relative newbie to the WooCommerce platform, I would appreciate if I could get a 'Yes, that works' or a 'No, I have missed something out' response to my below understanding of how the WooCommerce; files, hooks, actions and templates all work with one and other
My Understanding:
woocommerce_breadcrumb
entry.add_action('woocommerce_before_main_content','woocommerce_breadcrumb', 20, 0 );
This simply directs woocommerce_breadcrumb
to be called within the woocommerce_before_main_content
hook.do_action( 'woocommerce_before_main_content' );
where necessary. In this case, within all of the Template files.To do that just copy the WooCommerce template you need to customize and add it to your plugin folder. Now all the customizations you need to make to the WooCommerce cart page can be made in the 'cart. php' file in your plugin folder.
Most themes include the functions. php file in the main directory, e.g., Twenty-Twelve theme has one under wp-content/themes/twentytwelve/functions.
To override WooCommerce template files in your theme (or better yet, child theme) simply make a folder named 'woocommerce' within your theme directory, and then create the folders/template file you wish to override within it.
Your 1,2,3 understanding is correct.
However, the files wc-template-functions.php
and wc-templates-hooks.php
are not overridden by placing similar files in your theme, so having them in your theme would not do anything.
It's also, a bad idea (in my opinion) to wholesale copy/override files when you want to change something specific. I had to hunt through an entire folder of WooCommerce templates when a client's site crashed to find the actual changes that needed to be maintained.
There's not anything wrong with separating your functions.php
file into smaller, more manageable files. And so, you could have a woocommerce-functions.php
file named whatever you'd like to store your WooCommerce-specific code.
Edit to expand some thoughts
Anytime WooCommerce (or any WordPress function really) shows you this pattern:
if ( ! function_exists( 'some_function_name' ) ) {
function some_function_name() {
echo 'taco';
}
}
you have a pluggable function and you can just define it in your theme's function.php
and WooCommerce will use your version of some_function_name()
.
However, pluggable functions are hooked where they are hooked and you can't move them by redefining them in your theme/plugin. So a more powerful approach is to remove the function from it's hook and either add to back to a different hook, or add your own custom function, or both. Here's an example that moves a custom title to after the price:
function kia_switch_loop_title(){
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_after_shop_loop_item_title', 'kia_template_loop_product_title', 15 );
}
add_action( 'woocommerce_before_shop_loop_item', 'kia_switch_loop_title' );
function kia_template_loop_product_title() {
echo '<h4 class="we-do-what-we-want">' . get_the_title() . '</h4>';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With