Sha*_*aik 5 php wordpress email-templates orders woocommerce
我想在WooCommerce电子邮件模板中发送电子邮件时获取产品描述和产品名称.
我可以$order_id = trim(str_replace('#', '', $order->get_items()));使用此代码获取产品ID
但是当我试图获得它的描述和产品名称时,我无法做同样的事情.
我的代码:
$order = new WC_Order($order_id);
foreach($order->get_items() as $item){
$product_description = get_post($item['product_id'])->post_content;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使它工作?
谢谢
我在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');
}
Run Code Online (Sandbox Code Playgroud)
要做到这一点,你必须改变你的代码.
此外,我不确定您是否需要获取$order对象和订单ID作为$order对象已存在.
所以你可以在代码开头没有$order = new WC_Order($order_id);(或$order = wc_get_order( $order_id );)的情况下首先尝试.如果没有它,你只需再添加它.
这是代码:
$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
}
Run Code Online (Sandbox Code Playgroud)
此代码经过测试和运行.
代码位于活动子主题(或主题)的function.php文件中.或者也可以在任何插件php文件中.