根据 Woocommerce 中的用户角色自定义页脚文本电子邮件通知

Customizing footer text email notiications based on user role in Woocommerce

我正在尝试根据订购的用户是否是 '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'] 因为它不会 return 任何东西因为 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;
}

代码进入活动子主题(或活动主题)的 function.php 文件。

已经过测试并且有效。它也应该适合你。