我正在尝试根据订购的用户是否为"wholesale_customer"来编辑Woocommerce客户电子邮件。如果是,我想编辑页脚文本以显示不同的名称,并可能更改徽标,但此时名称更重要。
我正在使用 woocommerce_footer_text 函数尝试编辑,但当我测试它时,页脚文本没有显示。
谁能帮忙?
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页眉和页脚中可用。
您还应该$get_option['business']
签入代码,因为它不会返回任何内容get_option( 'woocommerce_email_footer_text' )
因为它不是数组,而是字符串,因此我删除了键['business']
。看到这是钩子源代码的摘录:
<?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;
}
代码进入函数.php活动子主题(或活动主题(的文件。
经过测试并工作。它也应该适合你。