WooCommerce动作挂钩和覆盖模板

时间:2016-08-21 12:03:59

标签: php wordpress templates woocommerce hook-woocommerce

我已经开始学习如何使用WooCommerce创建模板,我遇到了一些问题。例如,在Woocommerce插件的php文件content-single-product.php中,我有这样的字符串:

     <?php
        /**
         * woocommerce_single_product_summary hook.
         *
         * @hooked woocommerce_template_single_title - 5
         * @hooked woocommerce_template_single_rating - 10
         * @hooked woocommerce_template_single_price - 10
         * @hooked woocommerce_template_single_excerpt - 20
         * @hooked woocommerce_template_single_add_to_cart - 30
         * @hooked woocommerce_template_single_meta - 40
         * @hooked woocommerce_template_single_sharing - 50
         */
        do_action( 'woocommerce_single_product_summary' );

    ?>

例如,当我想编辑它(删除一些字段并更改结构)时,我尝试删除字符串:

  

do_action('woocommerce_single_product_summary');

然后写这样:

<?php
        /**
         * woocommerce_single_product_summary hook.
         *
         * @hooked woocommerce_template_single_title - 5
         * @hooked woocommerce_template_single_rating - 10
         * @hooked woocommerce_template_single_price - 10
         * @hooked woocommerce_template_single_excerpt - 20
         * @hooked woocommerce_template_single_add_to_cart - 30
         * @hooked woocommerce_template_single_meta - 40
         * @hooked woocommerce_template_single_sharing - 50
         */
        //do_action( 'woocommerce_single_product_summary' );
        do_action('woocommerce_template_single_title');
    ?>

你能告诉我为什么这不起作用?

这样编辑的正确方法是什么?

由于

1 个答案:

答案 0 :(得分:36)

  

首先在参考中,您将找到如何override properly woocommerce templates via a theme (避免编辑插件模板)

在您的第一个代码段中,正如您可以看到的 woocommerce_single_product_summary hook ,您可以按顺序使用 @hooked 的所有不同模板在 do_action() WordPress功能的挂钩位置中:

do_action( 'woocommerce_single_product_summary' ); 

因此,在您的自定义代码(第二个代码段)中,您刚刚用挂钩模板slug 替换了 hook (即 NOT 一个钩子并且 NOT 将作为入口点动作挂钩。请参阅本答案底部的参考文献list of WooCommerce actions and filters existing hooks ...

  

后果:评论列表代码中的所有其他已挂钩模板(以 @hooked 开头)将缺少如果您用模板slug替换挂钩< / strong>即可。

解释(如何)

  

如何 - 具体例子:

     

您希望 woocommerce_template_single_title hook 中自定义woocommerce_single_product_summary摘要模板

THE HOOK NAME:  woocommerce_single_product_summary hook.

THE TEMPLATES HOOKED (+priority order number)  => corresponding template file name:    
— woocommerce_template_single_title       (5) => single-product/title.php
— woocommerce_template_single_rating     (10) => single-product/rating.php
— woocommerce_template_single_price      (10) => single-product/price.php
— woocommerce_template_single_excerpt    (20) => single-product/short-description.php
— woocommerce_template_single_add_to_cart(30) => single-product/add-to-cart/ (6 files depending on product type)
— woocommerce_template_single_meta       (40) => single-product/review-meta.php
— woocommerce_template_single_sharing -  (50) => single-product/share.php
     

然后,您需要编辑 {{中相应的 woocommerce_single_product_summary摘要 title.php 1}} (子文件夹) ...一旦我们理解了模板结构文件和模板中的钩子,最后就不那么复杂了。

     

优先级编号,为挂钩模板提供订单:首先小一些,最后大一点......

另请参阅:Hooks and their hooked functions execution queue in Wordpress and Woocommerce

其他方式:

  

您还可以使用所有现有模板摘要来定位非常具体的更改或自定义,其中自定义函数位于您活动子项的 single-product 文件中主题(或主题)或任何插件文件。

使用function.php WordPress功能的示例:

add_action()
  

此功能的优先级编号为 // define the woocommerce_single_product_summary callback function function my_custom_action() { echo '<p>This is my custom action function</p>'; }; add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 ); ,并会显示   &#34;这是我的自定义操作功能&#34; 字符串文字,位于 15 product price ...

     

此挂钩函数的可选参数:
   - 模板slug(字符串)。
   - 优先级(int)。

参考文献: