Reputation: 75
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?
Upvotes: 3
Views: 2300
Reputation: 254373
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…
Upvotes: 3