简体   繁体   中英

Custom message on completed order status email notification only for customer user role

I would like to improve this code from this answer , to display a message on completed order status emails only for customers, but not for other user roles (as subscribers, etc...).

Here is that code:

add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {
    if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-completed' )
        echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}

How can I achieve it?

Thanks

To enable this custom message on email notification for complete order status and just for 'customer' user role , you have to get the user data related to the order, to get the user role. Then you will use it in your conditional.

Here is the code:

add_action( 'woocommerce_email_before_order_table', 'completed_order_mail_message', 20 );
function completed_order_mail_message( $order ) {

    // Getting order user data to get the user roles
    $user_data = get_userdata($order->customer_user);

    if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-completed' && in_array('customer', $user_data->roles) )
        echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and fully functional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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