3

After Woocommerce update to 3.2, This code below does not work anymore.

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('')
    $shipping_method_arr = get_post_meta($order_id, '_shipping_method', false); // an array
    $rate_id = $shipping_method_arr[0][0]; // the rate ID

    if ( 'flat_rate:10' == $rate_id ){
        echo pll__("Text 1");
    } else {
        echo pll__("Text 2");
    }
}

What is wrong or obsolete in this code?
What changes need to be done to make it work again?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Ugenijus
  • 75
  • 6

1 Answers1

3

Here is the correct way to get the Shipping methods used in the Order and make this function works as expected:

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;

    $found = false; // Initializing variable

    // Iterating through Order shipping methods
    foreach($order->get_shipping_methods() as $value){
        $rate_id = $value->get_method_id(); // Get the shipping rate ID
        if ( 'flat_rate:10' == $rate_id )
            $found = true;
    }

    if ($found)
        echo '<p>'.__("Text 1 (found)","woocommerce").'</p>';
    else
        echo '<p>'.__("Text 2 (else)","woocommerce").'</p>';
}

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

Tested and works…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello. It seems that code stops working after woocommerce version 3.4. Any thoughts how to fix code? – Ugenijus May 30 '18 at 09:04
  • @Ugenijus Ok I will need some time to check what is wrong and make an update. I will notify you very very soon. – LoicTheAztec May 30 '18 at 09:43
  • @Ugenijus It seems that there is a bug in Woocommerce 3.4 related to `woocommerce_email_order_details` action hook. If you try to display anything like a string (without any restrictions)… Nothing is displayed. I have try to use other email hooks and none is working. **So it's a bug that Woocommerce team will solve on next releases.** – LoicTheAztec May 30 '18 at 14:28
  • Do you think they have info about this bug? – Ugenijus May 31 '18 at 07:20
  • @Ugenijus I don't know, you can try to report it on Woocommerce Github… **But this are some Gold rules:** 1. woocommerce version 3.4 is a new major release so too young for production still with some bugs (that will be solved in the futures minor updates). 2. All plugins and themes are not yet fully compatible with it. 3. Always test updates before on a stage web site. 4. Always wait one or two month before applying a Woocommerce major release update. – LoicTheAztec May 31 '18 at 07:24
  • Hello @LoicTheAztec , maybe you can recheck this code? Code stoped after Woocommerce 3.4. – Ugenijus May 26 '22 at 18:39