Can I modify WooCommerce functions within the 'wc-template-functions.php' file or should I stick with the 'functions.php' file for such modifications?

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:

  1. The WooCommerce functions are registered within the 'wc-template-functions.php' file. For the purpose of this explanation, I would like to refer to the woocommerce_breadcrumb entry.
  2. WooCommerce then uses the 'wc-templates-hooks.php' file to call the registered function by using a typical entry such as 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.
  3. WooCommerce is then able to output the above by placing do_action( 'woocommerce_before_main_content' ); where necessary. In this case, within all of the Template files.

Solutions

was struggling with how to make related products sorting in desc way, just found solution. In case anybody needs it

function custom_remove_hook(){
    remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    add_action( 'woocommerce_after_single_product_summary', 'custom_function_to_sort_related', 22 );
}
add_action( 'woocommerce_after_single_product_summary', 'custom_remove_hook' );

function custom_function_to_sort_related( $args = array() ) {
        global $product;

        if ( ! $product ) {
            return;
        }

        $defaults = array(
            'posts_per_page' => 4,
            'columns'        => 4,
            'orderby'        => 'price', // @codingStandardsIgnoreLine.
            'order'          => 'desc'
        );

        $args = wp_parse_args( $args, $defaults );

        // Get visible related products then sort them at random.
        $args['related_products'] = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $args['posts_per_page'], $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );

        // Handle orderby.
        $args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] );

        // Set global loop values.
        wc_set_loop_prop( 'name', 'related' );
        wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_related_products_columns', $args['columns'] ) );

        wc_get_template( 'single-product/related.php', $args );
    }

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>';
}

Similar questions

Is it not recommendable to stick with plugins that are no longer supported by the author for production sites?
For example, I used the plugin Vote it Up but it haven't been updated since 2010-5-28. Now I moved to GD star rating which was last updated in January 6, 2011 (and still maintained by he author). Is it not recommendable to stick with plugins that are no longer supported by the author for production sites?
How to stick custom post at the top in search results
I'm trying to stick a post in the search results for the value and meta_key meta_value. If the search results in the post is meta_key - sticky and meta_value - fatured, the display on top of these posts. This is my search.php :
How to add "Stick this post to the front page" to front end?
I have a baseball related website with multiple authors. I use the "Stick this post to the front page" to denote an "Editor's Pick" on an article. I would like to add a link/button to allow editor to do this from the front end. The method can either be in the article itself or in the Admin Bar. I do not really have a preference. I have looked throu...
Trying to display stick or featured post on homepage
Im trying to add the last sticky post to a section on my homepage as a Featured article. I have created this so far and it displays posts but not the sticky ones: I have tried to change the query_posts to get_post. The problem then is that it displays the other 3 'latest news' posts from the other loop at the top of the page. Any help would be real...
How to keep a CPT stick to specific position?
I have created a CPT (esp-publicitarios) using Metabox plugin with the following code: That works perfect. I need to insert the adversiting in a given position (3, 6 or 9 as shown above) so I have made the following function: Next, this is how I use the code above in then front page: That "works" but I have a problem, because the CPT is tied to the...
Stick multiple posts in a single category
How to stick multiple posts in a single category to show on top of that category? Tried Ideas: I tried to use "Category Sticky Post" plugin, but it only allows me to pin one post for each category. Also tried to stick posts to the homepage, and remove the featured posts from the home page, but they don't stick on top of the category.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .