php WooCommerce 操作挂钩和覆盖模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39063958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:48:09  来源:igfitidea点击:

WooCommerce action hooks and overriding templates

phpwordpresstemplateswoocommercehook-woocommerce

提问by Dmitry Shulga

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:

我已经开始学习如何使用 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' );

    ?>

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

and after that write like this:

然后像这样写:

<?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?

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

Thanks

谢谢

回答by LoicTheAztec

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

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

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

在您的第一个代码片段中,正如您在woocommerce_single_product_summaryhook 中看到的那样,您可以使用WordPress 功能@hooked对该挂钩位置中的所有不同模板进行排序do_action()

do_action( 'woocommerce_single_product_summary' ); 

So in your customized code (the 2nd code snippet) you have just replaced the hook, by the hooked template slug(that is NOTa hook)and will NOTwork 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

因此,在您的自定义代码(第2的代码片段)你刚更换过,用钩模板蛞蝓(即不是一个钩)和将工作为切入点行动挂钩。有关WooCommerce 操作和过滤器现有挂钩列表,请参阅此答案底部的参考资料……

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.

结果:如果用模板 slug 替换钩子,注释列表代码(以@hooked开头)中的所有其他钩子模板都将丢失。



Explanations (How to):

说明(如何)

HOW TO - Concrete example:

You want to customize woocommerce_template_single_titlehooked templatein woocommerce_single_product_summaryhook.

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

Then you will need to editthe corresponding woocommerce_single_product_summaryhooktitle.phplocated 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…

如何 - 具体示例:

您想在hook 中自定义woocommerce_template_single_titlehooked 模板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.phpsingle-product

优先数,给出了钩模板的顺序:先中更小,在年底大...

See also: Hooks and their hooked functions execution queue in Wordpress and Woocommerce

另请参阅:Wordpress 和 Woocommerce 中的挂钩及其挂钩函数执行队列



Others ways:

其他方式:

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

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

Example using add_action()WordPress function:

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

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

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

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

这个函数的优先级序号15,并显示 “这是我的自定义操作功能”字符串文本,之间product priceproduct short description...

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



References:

参考: