Get Product Name and Description in WooCommerce email templates

旧街凉风 提交于 2019-12-05 11:09:17

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.

Angelo

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!