根据 Woocommerce 中的用户角色自定义页脚文本电子邮件通知 [英] Customizing footer text email notiications based on user role in Woocommerce
问题描述
我正在尝试根据订购的用户是否为wholesale_customer"来编辑 Woocommerce 客户电子邮件.如果他们是,我想编辑页脚文本以显示不同的名称,并可能更改徽标,但此时名称更为重要.
I'm trying to edit the Woocommerce customer emails based on whether the user who ordered was a 'wholesale_customer'. If they are i want to edit the footer text to display a different name and maybe change the logo but at this time the name is more important.
我正在使用 woocommerce_footer_text 函数尝试和编辑,但是当我测试它时,页脚文本没有显示.
I'm using the woocommerce_footer_text function to try and edit but when I test it, the footer text isn't displaying.
有人可以帮忙吗?
add_filter( 'woocommerce_email_footer_text', 'woocommerce_footer_text', 10, 2 );
function woocommerce_footer_text( $get_option, $order ) {
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
return $recipient;
}
// just in case
if ( ! $order instanceof WC_Order ) {
return $recipient;
}
//Get the customer ID
$customer_id = $order->get_user_id();
// Get the user data
$user_data = get_userdata( $customer_id );
// Adding an additional recipient for a custom user role
if ( in_array( 'wholesale_customer', $user_data->roles ) ) {
$get_option['business'] = 'Store 1';
} else {
$get_option['business'] = 'Store 2';
}
return $get_option;
}
推荐答案
在此钩子中没有可用的 $order
WC_Order
对象或订单 ID.但是可以将此电子邮件通知的当前订单 ID 设置为全局变量,然后在 $order 对象不存在的页眉和页脚中可用.
There is no $order
WC_Order
object or Order ID available in this hook. But it's possible to set the current Order ID for this email notification as a global variable and then it's available in header and footer where $order object doesn't exist.
您还应该检查您的代码 $get_option['business']
因为它不会返回任何内容作为 get_option( 'woocommerce_email_footer_text' )
不是数组,而是字符串,所以我删除了键 ['business']
.看到这是钩子源代码的摘录:
You should also check in your code $get_option['business']
as it will not return anything as get_option( 'woocommerce_email_footer_text' )
is not an array, but a string, so I have removed the key ['business']
. See that is an extract of the hook source code:
<?php echo wpautop( wp_kses_post( wptexturize( apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' ) ) ) ) ); ?>
这是重新访问的代码:
// Setting the Order ID as a global variable
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);
function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
$GLOBALS['order_id_str'] = $order->get_id();
}
// Conditionally customizing footer email text
add_action( 'woocommerce_email_footer_text', 'custom_email_footer_text', 10, 1 );
function custom_email_footer_text( $get_option ){
// Getting the email Order ID global variable
$refNameGlobalsVar = $GLOBALS;
$order_id = $refNameGlobalsVar['order_id_str'];
// If empty email Order ID we exit
if( empty($order_id) ) return;
//Get the customer ID
$user_id = get_post_meta( $order_id, '_customer_user', true );
// Get the user data
$user_data = get_userdata( $user_id );
if ( in_array( 'wholesale_customer', $user_data->roles ) ) {
$get_option = 'Store 1';
} else {
$get_option = 'Store 2';
}
return $get_option;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.
经过测试并有效.它也应该适合你.
Tested and works. It should also work for you.
这篇关于根据 Woocommerce 中的用户角色自定义页脚文本电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!