Update 2 - Added Compatibility WC from version 2.4 to 3.2+
Here is the correct way to make your code works (in woocommerce 2.6.x and above), keeping your function, that you will use in a second custom hooked function to get the display in email notifications:
// Your conditional function that output a custom text
// The argument needed is the WC_Order object (instead of the Order ID)
function advmi_check_order_product_id( $order )
{
$has_product1 = $has_product2 = false; // Initializing
// Added Compatibility WC from version 2.4 to 3.2+ (Get The order ID)
## $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
## $order = new WC_Order($order_id); // Get the WC_Order object
foreach( $order->get_items() as $item ) {
// Added Compatibility WC from version 2.4 to 3.2+
$product_id = method_exists( $item, 'get_product_id' ) ? $item->get_product_id() : $item['product_id'];
if( 56943 == $product_id ) $has_product1 = true;
if( 95956 == $product_id ) $has_product2 = true;
}
if ( $has_product1 && $has_product2 ) {
echo '<p>'.__('Added Text for both products in the order').'</p>
<p>'.__('Text').'</p>';
}
elseif ( $has_product1 && ! $has_product2 ) {
echo '<p>'.__('Text for only 56943').'</p>
<p>'.__('Text').'</p>';
}
}
// The email function hooked that display the text
add_action( 'woocommerce_email_order_details', 'add_text_conditionally', 10, 4 );
function add_text_conditionally( $order, $sent_to_admin, $plain_text, $email ) {
// For customer completed orders status only
if ( $sent_to_admin || ! $order->has_status('completed') ) return;
advmi_check_order_product_id( $order );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works on versions 2.6.14 and 3.2.3…