Shaun
Shaun

Reputation: 811

Customizing footer text email notiications based on user role in 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.

I'm using the woocommerce_footer_text function to try and edit but when I test it, the footer text isn't displaying.

Can anyone help please?

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;
}

Upvotes: 1

Views: 2911

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254362

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.

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' ) ) ) ) ); ?>

Here is the revisited code:

// 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;
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works. It should also work for you.

Upvotes: 4

Related Questions