我正在尝试在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;
}
我该如何运作?
谢谢
我在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');
}
为此,您必须更改一些代码。
另外,我不确定您是否需要获取$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
}
此代码已经过测试并且可以工作。
代码进入您的活动子主题(或主题)的function.php文件中。或在任何插件php文件中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句