dtcd27183 2017-02-20 00:19
浏览 197
已采纳

我可以修改'wc-template-functions.php'文件中的WooCommerce函数,还是应该坚持使用'functions.php'文件进行修改?

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.
  • 写回答

2条回答 默认 最新

  • duanshang7007 2017-02-20 01:08
    关注

    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>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包