在管理员中显示订单下载 WooCommerce 中的新订单电子邮件通知
Show order downloads in admin new order email notification in WooCommerce
如何在管理员电子邮件中包含此“下载”部分(随附)?默认情况下,WooCommerce 仅将其发送给客户,而不是店主。
我尝试查看介绍如何自定义 WooCommerce 电子邮件的文章,我 认为 woocommerce_email_order_details
是我正在寻找的钩子。但是,由于我无法找到如何实际使用此挂钩来更改电子邮件通知的内容,因此我只能使用这条信息。
此外,我还查看了 WooCommerce 包含的电子邮件模板:我注意到客户通知和管理员通知都有这一行 do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
两者是相同的,我假设它的行为不同取决于 $sent_to_admin
是否是 True
;然而,我还是没能找到如何使用它让电子邮件在客户和管理员电子邮件中包含下载部分。
有什么建议吗?
查看 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负责下载部分出现与否。
如果您希望它出现在管理员订单电子邮件中,我认为实现此目的的最简单方法是执行以下操作:
- 将 wp-content/plugins/woocommerce/templates/emails/admin-new-order.php 复制到您的 theme/woocommerce/emails 文件夹
- 改变这个:
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 );
这应该有所作为
使用位于 /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”值设置为假即可。您可以通过更新位于以下位置的以下文件来实现
/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 默认值,认为您可能有自定义订单元或电子邮件页脚部分仅供管理员使用。
虽然我没有在任何网站上测试过,但我希望它能正常工作。
此答案包含一个解决方案,无需覆盖模板文件
在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
,必须为真才能在table中显示订单下载。所以$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,
)
);
}
}
如何在管理员电子邮件中包含此“下载”部分(随附)?默认情况下,WooCommerce 仅将其发送给客户,而不是店主。
我尝试查看介绍如何自定义 WooCommerce 电子邮件的文章,我 认为 woocommerce_email_order_details
是我正在寻找的钩子。但是,由于我无法找到如何实际使用此挂钩来更改电子邮件通知的内容,因此我只能使用这条信息。
此外,我还查看了 WooCommerce 包含的电子邮件模板:我注意到客户通知和管理员通知都有这一行 do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
两者是相同的,我假设它的行为不同取决于 $sent_to_admin
是否是 True
;然而,我还是没能找到如何使用它让电子邮件在客户和管理员电子邮件中包含下载部分。
有什么建议吗?
查看 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负责下载部分出现与否。
如果您希望它出现在管理员订单电子邮件中,我认为实现此目的的最简单方法是执行以下操作:
- 将 wp-content/plugins/woocommerce/templates/emails/admin-new-order.php 复制到您的 theme/woocommerce/emails 文件夹
- 改变这个:
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 );
这应该有所作为
使用位于 /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”值设置为假即可。您可以通过更新位于以下位置的以下文件来实现 /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 默认值,认为您可能有自定义订单元或电子邮件页脚部分仅供管理员使用。 虽然我没有在任何网站上测试过,但我希望它能正常工作。
此答案包含一个解决方案,无需覆盖模板文件
在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
,必须为真才能在table中显示订单下载。所以$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,
)
);
}
}