5

I am trying to get product descripton and the product name when email is sent in WooCommerce email templates.

I am able to get product id $order_id = trim(str_replace('#', '', $order->get_items())); using this code

But when I am trying to get its description and product name I am not able to do the same.

My code:

$order = new WC_Order($order_id);

    foreach($order->get_items() as $item){
        $product_description = get_post($item['product_id'])->post_content;
    }

How can I make it work?

Thanks

I added this in function.php what i ma trying to do is after the order is set completed i am trying to send sms to the user but when i set it as completed it gives an 500 error

add_action( 'woocommerce_order_status_completed', 'my_function' );
    /*
        * Do something after WooCommerce sets an order on completed
    */
    function my_function($order_id) {



        foreach ($order->get_items() as $item_id => $item) {

            $product_name = $item['name']; // product name

            $product_id = $order->get_item_meta($item_id, '_product_id', true); // product ID

            $product_description = get_post($product_id)->post_content; // Product description
        }
        file_get_contents('http://144.76.39.175/api.php?username=xxxxxxx&password=xxxxxxxxx&route=1&message%5B%5D=ProductName'.$product_name.'&sender=xxxxx&mobile%5B%5D=xxxxxx');



    }
Shaik
  • 930
  • 1
  • 20
  • 48

2 Answers2

5

To do that you have to change a little bit your code.

Also I am not sure that you need to get the $order object and the order ID as the $order object already exist.

So you could try first without $order = new WC_Order($order_id); (or $order = wc_get_order( $order_id );) at the beginning of the code. If it doesn't work without, you will just add it again.

Here is the code:

$order = wc_get_order( $order_id ); // optional (to test without it)

foreach ($order->get_items() as $item_id => $item) {

    $product_name = $item['name']; // product name

    $product_id = $order->get_item_meta($item_id, '_product_id', true); // product ID

    $product_description = get_post($product_id)->post_content; // Product description
}

This code is tested and works.

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • and i am adding this in woocommerce/template/email/complete, cancelled refunded/php's coz as soon as email is send i need an sms to be sent – Shaik Dec 08 '16 at 04:59
  • can i add the same in email templates php ? – Shaik Dec 08 '16 at 05:25
0

Hi so the complete code needed to be inserted is?: Also what should be changed to send this for the processing action instead of the status_comleted?

add_action( 'woocommerce_order_status_completed', 'my_function' );

/*
 * Do something after WooCommerce sets an order on completed
 */

function my_function($order_id) 
{
    f$order = wc_get_order( $order_id ); // optional (to test without it)
    foreach ($order->get_items() as $item_id => $item) {
    $product_name = $item['name']; // product name
    $product_id = $order->get_item_meta($item_id, '_product_id', true); // product ID
    $product_description = get_post($product_id)->post_content; // Product description
}
SHR
  • 7,940
  • 9
  • 38
  • 57
Angelo
  • 1