4

How to add product description/content of single product page (not the short description) to WooCommerce new order email notification?

I need to know specific written description of my products as most of them are almost same.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
A. Rahman
  • 43
  • 1
  • 3

1 Answers1

6

As you are targeting a specific email notification, first we need to get the Email ID to target the "New Order" email notification. The only way is to get it before and to set the value in a global variable.

Then in a custom function hooked in woocommerce_order_item_meta_end action hook, we display the product description exclusively for New Order email notification.

Here is that code:

 ## Tested on WooCommerce 2.6.x and 3.0+

// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
    $GLOBALS['email_id_str'] = $email->id;
}

// Displaying product description in new email notifications
add_action( 'woocommerce_order_item_meta_end', 'product_description_in_new_email_notification', 10, 4 );
function product_description_in_new_email_notification( $item_id, $item, $order = null, $plain_text = false ){

    // Getting the email ID global variable
    $refNameGlobalsVar = $GLOBALS;
    $email_id = $refNameGlobalsVar['email_id_str'];

    // If empty email ID we exit
    if(empty($email_id)) return;

    // Only for "New Order email notification"
    if ( 'new_order' == $email_id ) {

        if( version_compare( WC_VERSION, '3.0', '<' ) ) { 
            $product_id = $item['product_id']; // Get The product ID (for simple products)
            $product = wc_get_product($item['product_id']); 
        } else {
            $product = $item->get_product();
            
            if( $product->is_type('variation') ) {
                $product = wc_get_product( $item->get_product_id() );
            }
        }

        // Get the Product description (WC version compatibility)
        if ( method_exists( $item['product'], 'get_description' ) ) {
            $product_description = $product->get_description(); // for WC 3.0+ (new)
        } else {
            $product_description = $product->post->post_content; // for WC 2.6.x or older
        }

        // Display the product description
        echo '<div class="product-description"><p>' . $product_description . '</p></div>';
    }
}

This 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.

Code Update and Error explanations in woocommerce_order_item_meta_end action hook:

PHP Warning for woocommerce_order_item_meta_end (Mike Joley)

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you for replying. It's working. I am receiving emails with product description now. However, after placing order, this message is displayed on the page: Warning: Missing argument 4 for product_description_in_new_email_notification() in /home/cbllookbook/public_html/wp-content/themes/you-child/functions.php on line 22 which is function product_description_in_new_email_notification( $item_id, $item, $order, $plain_text ){ should i rearrange the arguments? Thanks again. – A. Rahman Apr 23 '17 at 06:01
  • I'm not sure if it's the correct way to do this; however, I set the value of argument 4 to NULL so that I can get rid of the message. – A. Rahman Apr 23 '17 at 06:30
  • This only works on admin emails, not customer emails. – Michelle Jul 07 '23 at 14:07
  • @Michelle No sorry, it works on all emails. Now this code is old and it can be shortened, removing compatibility with prior versions of Woocommerce 3.0 – LoicTheAztec Jul 07 '23 at 16:47
  • https://stackoverflow.com/users/3730754/loictheaztec Any idea how i can do this https://stackoverflow.com/questions/76634373/custom-product-description-in-woocommerce – Michelle Jul 07 '23 at 16:51