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

时间:2021-06-22 08:43:31

标签: php wordpress woocommerce orders email-notifications

如何在管理员电子邮件中包含此“下载”部分(附件)?默认情况下,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 个答案:

答案 0 :(得分: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,
            )
        );
    }
}

答案 1 :(得分: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. 将 wp-content/plugins/woocommerce/templates/emails/admin-new-order.php 复制到您的主题/woocommerce/emails 文件夹中
  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 );

这应该有所作为

答案 2 :(得分: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 默认值,认为您可能只有管理员的自定义订单元或电子邮件页脚部分。 虽然我没有在任何网站上测试过这个,但我希望这能正常工作。