繁体   English   中英

根据 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']因为它不会返回任何内容,因为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 文件中。

测试和工作。 它也应该适合你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM