1

I try to fetch the product name of the order in woocommerce, now I can fetch the shipping method by using :

add_action( 'woocommerce_email_after_order_table', 'wdm_add_shipping_method_to_order_email', 10, 2 );
function wdm_add_shipping_method_to_order_email( $order, $is_admin_email ) {
    echo '<p><h4>Shipping:</h4> ' . $order->get_shipping_method() . '</p>';
}

I tried to use:

$order->get_name() or $order->get_id() 

But it shows internal server error. please help.

How to fetch the name or the ID of the product in order email notifications?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Joey Wang
  • 11
  • 6
  • try `$order->id();` – dipak_pusti Feb 21 '18 at 03:45
  • @dipak_pusti That is wrong… In woocommerce 3+ to get the Order ID you use `$order->get_id();` and for product ID `$product->get_id();`… As you can see this question is asking for product ID or product name… – LoicTheAztec Feb 21 '18 at 03:57

1 Answers1

1

As an order can have many items you can have many products names in an order. You need to get order items first and to loop through each one to get the product names or IDs.

This can be done using the following (that will replace your actual code):

add_action( 'woocommerce_email_after_order_table', 'custom_email_after_order_table', 10, 4 );
function custom_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {

    // Displaying the shipping method used
    echo '<p><h4>'.__('Shipping', 'woocommerce').':</h4> '.$order->get_shipping_method().'</p>';

    $product_names = array();

    // Loop thougth order items
    foreach( $order->get_items() as $item_id => $item ){
        $product = $item->get_product(); // Get an instance of the WC_Product object
        $product_id = $item->get_product_id(); // Get the product ID

        // Set each product name in an array
        $product_names[] = $item->get_name(); // Get the product NAME
    }
    // Displaying the product names
    echo '<p><strong>'.__('Product names', 'woocommerce').':</strong> <br>'.implode( ', ', $product_names ).'</p>';
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi Loic, thanks for the reply, I was wondering if there is only one item in the order, and the name of the item is already shows on the email receipt, I just want to fetch the name already shows on the email. How can I do that? – Joey Wang Feb 21 '18 at 04:31
  • and so far the code you provided is amazing!! thanks so much! – Joey Wang Feb 21 '18 at 04:35
  • @JoeyWang If there is only one item, `implode( ', ', $product_names )` code will output that unique name anyway. Even with only one item, you need to use a foreach loop anyway, to get that item properties using `WC_Order_Item_Product` or `WC_Product` **methods** – LoicTheAztec Feb 21 '18 at 04:37
  • Yes, I have accept it, may I please ask additional question? can I fetch the ID of the product? – Joey Wang Feb 21 '18 at 04:50
  • @JoeyWang You can use the variable `$product_id` outside the foreach loop and you will get the last item product ID (so as you have only one item, that is not a problem). – LoicTheAztec Feb 21 '18 at 05:08