简体   繁体   English

在WooCommerce电子邮件模板中获取产品名称和描述

[英]Get Product Name and Description in WooCommerce email templates

I am trying to get product descripton and the product name when email is sent in WooCommerce email templates. 我正在尝试在WooCommerce电子邮件模板中发送电子邮件时获取产品描述和产品名称。

I am able to get product id $order_id = trim(str_replace('#', '', $order->get_items())); 我能够获得产品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 我在function.php中添加了此命令,我想做的是在订单设置完成后,我试图向用户发送短信,但是当我将其设置为完成时,则出现500错误

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');



    }

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. 另外我不确定您是否需要获取$order对象和订单ID,因为$order对象已经存在。

So you could try first without $order = new WC_Order($order_id); 因此,您可以先尝试使用$order = new WC_Order($order_id); (or $order = wc_get_order( $order_id ); ) at the beginning of the code. (或$order = wc_get_order( $order_id ); )在代码开头。 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). 代码进入您的活动子主题(或主题)的function.php文件中。 Or also in any plugin php files. 或在任何插件php文件中。

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? 嗨,您需要插入的完整代码是吗?:还应该更改什么以将其发送给处理操作而不是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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM