将基于用户角色的附件添加到 WooCommerce 新用户注册电子邮件
Add attachments based on user role to WooCommerce new user registration email
我想添加两个pdf文件到WooCommerce的新用户注册邮箱。
因为我有两个特定的用户角色 customer
和 seller
。
我想向所有新卖家发送来自路径 $path1
和 $path3
的两个 pdf 文件,向所有新客户发送来自路径 $path2
和 $path3
.
的两个 pdf 文件
我在 functions.php
中试过了
function attach_to_email ( $attachments, $userrole ) {
$root = ABSPATH;
$path1 = $root . '/media/AGB H.pdf';
$path2 = $root . '/media/AGB K.pdf';
$path3 = $root . '/media/W.pdf';
if ( $userrole === 'seller' ) {
$attachments[] = $path1;
$attachments[] = $path3;
} else {
$attachments[] = $path2;
$attachments[] = $path3;
}
return $attachments;
}
add_filter( 'woocommerce_email_attachments', 'attach_to_email', 10, 2 );
在卖家的电子邮件模板中,我调用:
do_action( 'woocommerce_email_attachments', null, 'seller' );
但是在函数中,我总是输入else部分而不是if部分。此外,现在所有电子邮件都附有 else 文件,而不仅仅是注册电子邮件。有什么想法吗?
要仅将附件分配给注册电子邮件,您可以使用:
$email_id
,这里等于customer_new_account
附件路径,链接主题,可以使用:
get_stylesheet_directory()
为儿童主题
get_template_directory()
父主题
- 我还建议不要在 pdf 文件的文件名中使用空格
然后您可以根据用户角色分配正确的附件
所以你得到:
function filter_woocommerce_email_attachments( $attachments, $email_id, $object, $email_object = null ) {
// Use get_stylesheet_directory() for a child theme
// Use get_template_directory() for a parent theme
$path_1 = get_template_directory() . '/my-file-1.pdf';
$path_2 = get_template_directory() . '/my-file-2.pdf';
$path_3 = get_template_directory() . '/my-file-3.pdf';
// Customer new account email
if ( isset( $email_id ) && $email_id === 'customer_new_account' ) {
// Get user role(s)
$roles = (array) $object->roles;
// Seller
if ( in_array( 'seller', $roles ) ) {
$attachments[] = $path_1;
$attachments[] = $path_3;
// Customer
} elseif ( in_array( 'customer', $roles ) ) {
$attachments[] = $path_2;
$attachments[] = $path_3;
}
}
return $attachments;
}
add_filter( 'woocommerce_email_attachments', 'filter_woocommerce_email_attachments', 10, 4 );
代码进入您活动主题的 functions.php
文件。在 WooCommerce 5.0.0
中测试
我想添加两个pdf文件到WooCommerce的新用户注册邮箱。
因为我有两个特定的用户角色 customer
和 seller
。
我想向所有新卖家发送来自路径 $path1
和 $path3
的两个 pdf 文件,向所有新客户发送来自路径 $path2
和 $path3
.
我在 functions.php
function attach_to_email ( $attachments, $userrole ) {
$root = ABSPATH;
$path1 = $root . '/media/AGB H.pdf';
$path2 = $root . '/media/AGB K.pdf';
$path3 = $root . '/media/W.pdf';
if ( $userrole === 'seller' ) {
$attachments[] = $path1;
$attachments[] = $path3;
} else {
$attachments[] = $path2;
$attachments[] = $path3;
}
return $attachments;
}
add_filter( 'woocommerce_email_attachments', 'attach_to_email', 10, 2 );
在卖家的电子邮件模板中,我调用:
do_action( 'woocommerce_email_attachments', null, 'seller' );
但是在函数中,我总是输入else部分而不是if部分。此外,现在所有电子邮件都附有 else 文件,而不仅仅是注册电子邮件。有什么想法吗?
要仅将附件分配给注册电子邮件,您可以使用:
$email_id
,这里等于customer_new_account
附件路径,链接主题,可以使用:
get_stylesheet_directory()
为儿童主题get_template_directory()
父主题- 我还建议不要在 pdf 文件的文件名中使用空格
然后您可以根据用户角色分配正确的附件
所以你得到:
function filter_woocommerce_email_attachments( $attachments, $email_id, $object, $email_object = null ) {
// Use get_stylesheet_directory() for a child theme
// Use get_template_directory() for a parent theme
$path_1 = get_template_directory() . '/my-file-1.pdf';
$path_2 = get_template_directory() . '/my-file-2.pdf';
$path_3 = get_template_directory() . '/my-file-3.pdf';
// Customer new account email
if ( isset( $email_id ) && $email_id === 'customer_new_account' ) {
// Get user role(s)
$roles = (array) $object->roles;
// Seller
if ( in_array( 'seller', $roles ) ) {
$attachments[] = $path_1;
$attachments[] = $path_3;
// Customer
} elseif ( in_array( 'customer', $roles ) ) {
$attachments[] = $path_2;
$attachments[] = $path_3;
}
}
return $attachments;
}
add_filter( 'woocommerce_email_attachments', 'filter_woocommerce_email_attachments', 10, 4 );
代码进入您活动主题的 functions.php
文件。在 WooCommerce 5.0.0