【问题标题】:Customizing footer text email notiications based on user role in Woocommerce根据 Woocommerce 中的用户角色自定义页脚文本电子邮件通知
【发布时间】:2018-06-22 01:13:43
【问题描述】:

我正在尝试根据订购的用户是否为“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;
}

【问题讨论】:

    标签: php wordpress woocommerce user-roles email-notifications


    【解决方案1】:

    此挂钩中没有可用的 $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 文件中。

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

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2021-05-09
      • 2022-10-05
      • 2020-08-09
      • 2019-03-12
      • 2018-01-07
      • 2017-05-19
      • 2019-07-18
      • 1970-01-01
      相关资源
      最近更新 更多