在WooCommerce中的管理员新订单电子邮件通知中显示订单下载。

3
如何在管理员电子邮件中包含“下载”部分?默认情况下,WooCommerce只会将其发送给客户,而不是商店所有者。
我尝试查看了一些文章,展示了如何自定义WooCommerce电子邮件,我认为woocommerce_email_order_details是我要找的钩子。但是,我卡在这个信息上,因为我找不到如何实际使用此钩子来更改电子邮件通知内容。
此外,我还查看了WooCommerce包含的电子邮件模板:我注意到客户通知和管理员通知都有这行代码do_action('woocommerce_email_order_details',$order,$sent_to_admin,$plain_text,$email)。
它在两个通知中都是相同的,我假设它的行为取决于$sent_to_admin是否为True; 但是,我仍然无法找到如何使用它来使电子邮件在客户和管理员电子邮件中都包含“下载”部分的方法。
有什么建议吗?

enter image description here

3个回答

2
这篇答案提供了一种解决方案,无需覆盖模板文件
includes/class-wc-emails.php中,我们可以找到。
/**
 * Constructor for the email class hooks in all emails that can be sent.
 */
public function __construct() {
    $this->init();

    // Email Header, Footer and content hooks.
    add_action( 'woocommerce_email_header', array( $this, 'email_header' ) );
    add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) );
    add_action( 'woocommerce_email_order_details', array( $this, 'order_downloads' ), 10, 4 );
    ...

正如您所看到的,add_action 包含对 order_downloads() 函数的回调。

/**
 * Show order downloads in a table.
 *
 * @since 3.2.0
 * @param WC_Order $order         Order instance.
 * @param bool     $sent_to_admin If should sent to admin.
 * @param bool     $plain_text    If is plain text email.
 * @param string   $email         Email address.
 */
public function order_downloads( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );

    if ( ! $show_downloads ) {
        return;
    }

这个函数包含一个条件 $show_downloads,它必须为真才能在表格中显示订单下载。因此,$sent_to_admin 应该为假,以满足您的需求。


所以回答您的问题。在不覆盖模板文件的情况下,使用:

// Let 3rd parties unhook via this hook.
function action_woocommerce_email( $emails ) {
    // Removes a function from a specified action hook.
    remove_action( 'woocommerce_email_order_details', array( $emails, 'order_downloads' ), 10 );
    
    // Hooks a function on to a specific action.
    add_action( 'woocommerce_email_order_details', 'action_woocommerce_email_order_details', 9, 4 );
}
add_action( 'woocommerce_email', 'action_woocommerce_email', 10, 1 );

/**
 * Show order downloads in a table.
 *
 * @since 3.2.0
 * @param WC_Order $order         Order instance.
 * @param bool     $sent_to_admin If should sent to admin.
 * @param bool     $plain_text    If is plain text email.
 * @param string   $email         Email address.
 */
function action_woocommerce_email_order_details( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {   
    // Only for 'New Order' email notifications
    if ( $email->id == 'new_order' ) {
        $sent_to_admin = false;
    }

    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );

    if ( ! $show_downloads ) {
        return;
    }

    $downloads = $order->get_downloadable_items();
    
    $columns   = apply_filters(
        'woocommerce_email_downloads_columns',
        array(
            'download-product' => __( 'Product', 'woocommerce' ),
            'download-expires' => __( 'Expires', 'woocommerce' ),
            'download-file'    => __( 'Download', 'woocommerce' ),
        )
    );

    if ( $plain_text ) {
        wc_get_template(
            'emails/plain/email-downloads.php',
            array(
                'order'         => $order,
                'sent_to_admin' => $sent_to_admin,
                'plain_text'    => $plain_text,
                'email'         => $email,
                'downloads'     => $downloads,
                'columns'       => $columns,
            )
        );
    } else {
        wc_get_template(
            'emails/email-downloads.php',
            array(
                'order'         => $order,
                'sent_to_admin' => $sent_to_admin,
                'plain_text'    => $plain_text,
                'email'         => $email,
                'downloads'     => $downloads,
                'columns'       => $columns,
            )
        );
    }
}

非常感谢!只是想澄清一下,上面的代码放在我的子主题的“functions.php”中对吗?我有点新手,不明白如何在此文件中访问“wc_get_template”?您能再解释一下或指导我学习的正确方向吗?如果这只是放在我的子主题的“functions.php”文件中,那么我也可以自己查找更多信息。再次感谢! - nchopra
1
@nchopra 代码放在当前子主题(或活动主题)的functions.php文件中。我的答案的基本作用是,覆盖现有代码(请参见此处),使用我们自己的代码。这大致相同,只是如果涉及新订单管理员邮件,则将$send_to_admin设置为false。 - 7uc1f3r

1
在查看Woocommerce代码时,您可以在wp-content/plugins/woocommerce/includes/class-wc-emails.php中找到以下代码段:
/**
 * Show order downloads in a table.
 *
 * @since 3.2.0
 * @param WC_Order $order         Order instance.
 * @param bool     $sent_to_admin If should sent to admin.
 * @param bool     $plain_text    If is plain text email.
 * @param string   $email         Email address.
 */
public function order_downloads( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );

    if ( ! $show_downloads ) {
        return;
    }
    ...

这正好引出了你所想的,$sent_to_admin负责下载部分是否出现。

如果你希望在管理员订单电子邮件中显示它,我认为最简单的方法是执行以下操作:

  1. 在theme/woocommerce/emails文件夹中复制wp-content/plugins/woocommerce/templates/emails/admin-new-order.php
  2. 将以下内容更改为:

do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

更改为:

// Forcing email to be like the one the customer receives
do_action( 'woocommerce_email_order_details', $order, false, $plain_text, $email );

这应该会有所不同。

0

通过位于 /wp-content/plugins/woocommerce/includes/class-wc-emails.php 的此函数加载下载订单模板。

public function order_downloads( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
    $show_downloads = $order->has_downloadable_item() && $order->is_download_permitted() && ! $sent_to_admin && ! is_a( $email, 'WC_Email_Customer_Refunded_Order' );

    if ( ! $show_downloads ) {
        return;
    }
.....

如你所见,你需要使条件为真。如果所有其他条件都为真,那么你只需要将“$sent_to_admin”值设为false。你可以通过更新以下文件来实现这一点 /wp-content/themes/your-theme/woocommerce/emails/admin-new-order.php

替换

 do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

$store_sent_to_admin = $sent_to_admin;
$sent_to_admin = false;
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
$sent_to_admin = $store_sent_to_admin;

在这里,我已经存储了 $sent_to_admin 的默认值,考虑到您可能有自定义订单元数据或仅供管理员使用的电子邮件页脚部分。 虽然我还没有在任何网站上测试过这个功能,但我希望它能正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接