4

I'm trying to add different content to woocommerce completed order email notifications based on combinations of payment methods and shipping method.

My code so far:

// completed order email instructions

function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    if (( get_post_meta($order->id, '_payment_method', true) == 'cod' ) && ( get_post_meta($order->id, '_shipping_method', true) == 'local pickup' )){
    echo "something1";
} 
    elseif (( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) && ( get_post_meta($order->id, '_shipping_method', true) == 'local pickup' )){
    echo "something2";
 }
    else {
    echo "something3";
 }} 

The payment part works (I get the right "something1" to "something3" content) but if I add the && shipping condition, I get "something3" with every payment method.

Any idea what's wrong and how could I make it work?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Anna
  • 83
  • 1
  • 9

1 Answers1

4

There is multiple little things to change (as post meta payment method is an array for example):

// (Added this missing hook in your code)
add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {

    // Only for "Customer Completed Order" email notification
    if( 'customer_completed_order' != $email->id ) return;

    // Comptibility With WC 3.0+
    if ( method_exists( $order, 'get_id' ) ) {
        $order_id = $order->get_id();
    } else {
        $order_id = $order->id;
    }
    //$order->has_shipping_method('')
    $payment_method = get_post_meta($order_id, '_payment_method', true);
    $shipping_method_arr = get_post_meta($order_id, '_shipping_method', false); // an array
    $method_id = explode( ':', $shipping_method_arr[0][0] );
    $method_id = $method_id[0];  // We get the slug type method


    if ( 'cod' == $payment_method && 'local_pickup' == $method_id ){
        echo "something1";
    } elseif ( 'bacs' == $payment_method && 'local_pickup' == $method_id ){
        echo "something2";
    } else {
        echo "something3";
    }
}

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

This code is tested and works with WooCommerce version 2.6.x and 3+

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks and follow up question:I use the code as part of a custom plugin because I have to add different content to almost every type of customer email. I list the functions in the plugin and call them in the respective email templates in my child theme directly(I had to modify the templates anyway). I pasted the relevant part of your code to the plugin and it works.The other functions are based only on payment method,and my code above seemed to work so far. My question is: is it safe to leave them as they are or are there some 'hidden traps', and I should better modify them based on your code? – Anna Jul 03 '17 at 14:13
  • @Anna If you are using WooCommerce 3.+ you should use `$order->get_id()` instead in `get_post_meta()` … If some part of your code works just use them as they are… – LoicTheAztec Jul 03 '17 at 14:32