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
Code Revised (2023)
There are multiple mistakes in your code… Try the following instead:
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;
// Targeting Local pickup shipping method
if ( $order->has_shipping_method('local_pickup') ){
if ( 'cod' == $order->get_payment_method() ){
echo '<p>' . __("Custom text 1") . '</p>';
} elseif ( 'bacs' == $order->get_payment_method() ){
echo '<p>' . __("Custom text 2") . '</p>';
} else {
echo '<p>' . __("Custom text 3") . '</p>';
}
}
}
Code goes in functions.php file of your child theme (or in a plugin).
This code is tested and works with WooCommerce 3 and above.
Related: