WooCommerce动作挂钩和覆盖模板 [英] WooCommerce action hooks and overriding templates

查看:302
本文介绍了WooCommerce动作挂钩和覆盖模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have started to learn how to create templates with WooCommerce and I had faced with a little problem. For instance, in the php file content-single-product.php of Woocommerce plugin I have strings like that:

     <?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' );

    ?>

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

And for example, when I want to edit this (delete some fields and change the structure) I try erase the string:

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');
    ?>

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

Could you tell me please why this doesn't work?

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

What is the right way to edit like that?

谢谢

推荐答案

首先在下面的参考中,您将找到如何 通过主题正确覆盖woocommerce模板 (避免编辑插件模板).

First in reference below you will find how to override properly woocommerce templates via a theme (avoiding editing the plugin templates).

在您的第一个代码段中,正如您看到的 woocommerce_single_product_summary钩子所示,您在此中按顺序排列了所有 @hooked 的不同模板.具有 do_action() WordPress功能的>钩子位置" :

In your first code snippet, as you can see for woocommerce_single_product_summary hook, you have in order all the different templates that are @hooked in this hook location with do_action() WordPress function:

do_action( 'woocommerce_single_product_summary' ); 

因此,在您的自定义代码(第二个代码段)中,您刚刚将钩子替换为钩住的模板块 (即 NOT 一个钩子),并且不能用作入口点动作钩子.请参阅此答案底部的参考,以获取 WooCommerce操作和过滤器列表现有的钩子

So in your customized code (the 2nd code snippet) you have just replaced the hook, by the hooked template slug (that is NOT a hook) and will NOT work as an entry point action hook. See the references at the bottom of this answer for the list of WooCommerce actions and filters existing hooks

后果:如果您将钩子替换为模板块,则注释列表代码中所有其他钩子模板(以 @hooked 开头)都将丢失. /strong>.

Consequences: All other hooked templates in the commented list code (beginning with @hooked) will be missing if you replace a hook by a template slug.


说明(操作方法):


Explanations (How to):

操作方法-具体示例:

您要 woocommerce_single_product_summary挂钩中自定义woocommerce_template_single_title挂钩模板.

You want to customize woocommerce_template_single_title hooked template in woocommerce_single_product_summary hook.

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

然后,您需要编辑位于 single-product woocommerce_single_product_summary钩子 title.php > (子文件夹) ...一旦我们了解了模板结构文件和模板中的钩子,最后并没有那么复杂.

Then you will need to edit the corresponding woocommerce_single_product_summary hook title.php located in single-product (sub folder)… Finally is not so complicated, once we understand the template structure files and the hooks in that templates.

优先级数字给出了所钩模板的顺序:首先较小,最后较大...

The priority number, gives the order for the hooked templates: Smaller in first, bigger at the end…

另请参阅: 其他方式:

您还可以使用所有现有模板的钩子,以非常具体的更改或自定义为目标,并使用位于活动子主题的 function.php 文件中的自定义功能(或主题)或任何插件文件.

You can also use all that existing templates hooks to target very specific changes or customizations, with custom functions located in the function.php file of your active child theme (or theme) or any plugin file too.

使用add_action() WordPress功能的示例:

Example using add_action() WordPress function:

// 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 ); 

此功能的优先级编号为 15 ,并将显示 这是我的自定义操作功能" 字符串文本,位于 product price product short description

This function has a priority number of 15 and will display "This is my custom action function" string text, between the product price and the product short description

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

Optional arguments of this hooked function for this hook:
- The template slug (string).
- The priority (int).


参考文献:


References:

  • Template Structure + Overriding Templates via a Theme
  • Hooks: Action and Filter reference
  • LIST of WooCommerce Action and Filter (Hook Reference)
  • Template Structure + Overriding Templates via a Theme
  • WooCommerce Visual Hook Guide: Single Product Page

这篇关于WooCommerce动作挂钩和覆盖模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆