【问题标题】:Best practice to add elements to email sent by Woocommerce向 Woocommerce 发送的电子邮件添加元素的最佳实践
【发布时间】:2021-09-30 04:51:19
【问题描述】:

我是 Woocommerce 的新手,我知道很多语言...除了 PHP!此外,我不太了解 Wordpress 挂钩和回调是如何工作的,所以对于我想做的任务,我很迷茫。

我唯一想做的就是在订单完成后发送的电子邮件中添加一个二维码。

我首先理解的是我必须覆盖文件customer-completed-order.php 并将其放入我的主题文件夹中。

问题是,这个文件就是这样的:

<?php
/**
 * Customer completed order email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-completed-order.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates\Emails
 * @version 3.7.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/*
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
s

<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $order->get_billing_first_name() ) ); ?></p>
<p><?php esc_html_e( 'We have finished processing your order.', 'woocommerce' ); ?></p>
<?php

/*
 * @hooked WC_Emails::order_details() Shows the order details table.
 * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
 * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
 * @since 2.5.0
 */
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::order_meta() Shows order meta data.
 */
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

/*
 * @hooked WC_Emails::customer_details() Shows customer details
 * @hooked WC_Emails::email_address() Shows email address
 */
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );



/**
 * Show user-defined additional content - this is set in each email's settings.
 */
if ( $additional_content ) {
    echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}

/*
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );

这似乎不是添加我的回调的地方。我所做的是将此代码添加到我的主题中的functions.php,我想知道这是否是个好地方,以及我的代码是否也可以接受(为了简单起见,我暂时没有使用二维码):

add_action( 'woocommerce_email_before_order_table', 'add_qr_code_to_email', 20, 4 );
  
function add_qr_code_to_email( $order, $sent_to_admin, $plain_text, $email ) {
   if ( $email->id == 'customer_completed_order' ) {
      echo '<h2 class="email-upsell-title">Get 20% off</h2><p class="email-upsell-p">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
   }
}

最后,我的最后一个问题是(这不是最重要的):为什么它会起作用?我认为在customer-completed-order.php 中使用“do_action”“调用回调”是强制性的,但似乎我的函数仍然被调用

【问题讨论】:

    标签: php wordpress email woocommerce hook


    【解决方案1】:
    1. 没错。您不向模板文件添加函数。您在 functions.php 中添加它们,然后从模板中运行它们。

    2. 您的代码似乎没问题。一种常见的方法是创建不同的文件并将它们导入您的functions.php。但这是为了更容易维护和可读性。您可以在this repo 中查看如何完成此操作的示例

    3. 为什么会起作用?这实际上是最重要的问题,因为它是理解 WP 和 WC 开发如何工作的关键。


    如果您仔细查看要覆盖的文件,它几乎只是输出标记/内容并执行 WooCommerce 内置的操作 (do_action)。

    我建议阅读文档(请参阅最后的链接),但要简单地回答您的问题:

    操作允许我们在执行 WordPress 核心、插件和主题的特定点运行函数。换句话说,Action Hook 是 Wordpress 让我们能够扩展或修改核心功能的方式。

    可以为这些操作分配不同的回调函数。这些回调函数将在它们挂钩的操作运行时运行。

    所以回来,您模板中的每个 do_action() 函数都在运行一个操作,该操作仅运行与其挂钩的所有函数(在本例中为 WooCommerce)。

    所以,之所以有效,是因为您将您的函数与'woocommerce_email_before_order_table' 操作挂钩。

    如果您阅读上面每个do_action 的cmets,它实际上说明了它们与什么相关联。 'woocommerce_email_order_details' 操作的注释,在您的模板中,表示它已挂钩 WC_Emails::order_details() 并输出订单表。

    现在,那个函数正在获取一个模板文件并渲染它。您在代码中挂钩的操作在该模板中运行。 (有关该类及其方法,请参见wp-content/plugins/woocommerce/includes/class-wc-emails.php 的第 403 行)。

    希望我已经足够清楚了。

    更新了资源列表以了解更多信息:

    P.s:动作是 WP 中 2 种类型的钩子中的一种。还有过滤器。

    【讨论】:

    • 非常感谢,已经很清楚了。最后一件事我不明白,为什么在我的帖子(customer-completed-order.php)中显示的模板中,他们必须用 do_action 手动调用他们的回调?回调的目的不是在精确的时刻被调用吗?
    • 你用 do_action 手动调用他们的回调是什么意思?我不认为它们是,do_action 函数的第一个参数是钩子名称,它的作用是执行附加到它的所有函数。其他参数是可选的,并且是传递给回调函数的附加参数。这些回调函数需要使用add_action 函数添加到该钩子中。阅读:do_actionadd_action官方文档。
    猜你喜欢
    • 2010-12-10
    • 2011-12-11
    • 2011-02-16
    • 2019-06-26
    • 2011-07-08
    • 2020-07-31
    • 2011-08-20
    • 2013-01-22
    • 1970-01-01
    相关资源
    最近更新 更多