在 WooCommerce 电子邮件模板中获取产品名称和描述 [英] Get Product Name and Description in WooCommerce email templates

查看:71
本文介绍了在 WooCommerce 电子邮件模板中获取产品名称和描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WooCommerce 电子邮件模板中发送电子邮件时获取产品描述和产品名称.

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

我可以使用此代码获取产品 ID $order_id = trim(str_replace('#', '', $order->get_items()));

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.

我的代码:

$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?

谢谢

我在function.php中添加了这个我想做的是在订单设置完成后我试图向用户发送短信但是当我将其设置为完成时它给出了500错误

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



    }

推荐答案

为此,您必须稍微更改代码.

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

此外,我不确定您是否需要将 $order 对象和订单 ID 作为 $order 对象已经存在.

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

所以你可以先尝试没有 $order = new WC_Order($order_id); (或 $order = wc_get_order( $order_id );) 在代码的开头.如果没有它不起作用,你只需再次添加它.

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.

代码如下:

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

代码位于活动子主题(或主题)的 function.php 文件中.或者也可以在任何插件 php 文件中.

这篇关于在 WooCommerce 电子邮件模板中获取产品名称和描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆