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

查看:29
本文介绍了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<的所有不同模板进行了排序/code> 在这个 挂钩位置 中使用 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' ); 

因此,在您的自定义代码(第二个代码段)中,您刚刚将 hook 替换为 hooked 模板 slug (即 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 开头)中的所有其他挂钩模板都将丢失如果您用模板 slug 替换挂钩.

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.

有关模板中使用的钩子,请参阅此有用的WooCommerce Visual Hook 指南

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

然后你需要编辑对应的woocommerce_single_product_summary hook title.php> 位于single-product (子文件夹)... 最后不是那么复杂,一旦我们了解了模板结构文件和其中的钩子模板.

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…

另见:Wordpress 和 Woocommerce 中的钩子及其挂钩函数执行队列

您还可以使用所有现有模板挂钩来定位非常具体的更改或自定义,自定义函数位于 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,将显示这是我的自定义操作函数" 字符串文本,位于产品价格产品简短描述之间代码>...

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

可选参数:
• 模板 slug (string).
• 优先级(int).

参考文献:

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

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