简体   繁体   English

从 WooCommerce 中的另一个脚本添加客户备注

[英]Add customer note from another script in WooCommerce

I have a script that changes the status of orders based on our ERP system.我有一个脚本可以根据我们的 ERP 系统更改订单状态。

In addition to that, we need to add customer notes.除此之外,我们还需要添加客户备注。 I found the way to do it:我找到了这样做的方法:

$order->add_order_note($note);
$order->save();

Unfortunately this won't work outside the order edit screen, I tried to run it from my custom plugin.不幸的是,这在订单编辑屏幕之外不起作用,我尝试从我的自定义插件中运行它。 ( source ) 来源

If I do it via $order->update_status($status, $note);如果我通过$order->update_status($status, $note); it only updates the status.它只更新状态。

Is there a way to add a note outside the edit screen?有没有办法在编辑屏幕外添加注释? (Including e-mailing the customer) (包括给客户发电子邮件)

If the note is for the customer (and has to be visible for him) you need to use instead the WC_Order method set_customer_note() (or both) :如果注释是给客户的(并且必须对他可见),您需要使用WC_Order方法set_customer_note() (或两者)

$order->set_customer_note($note);
// $order->add_order_note($note);
$order->save();

Or:或者:

$order->set_customer_note($note);
$order->update_status($status, $note);

This need to be done before saving the order data or updating the order status.这需要在保存订单数据或更新订单状态之前完成。


To re-send the email notification to the customer (if needed) you can use from the current order ID:要将电子邮件通知重新发送给客户(如果需要),您可以使用当前订单 ID:

$emails = WC()->mailer()->get_emails();

$emails['WC_Email_Customer_Completed_Order']->trigger( $order_id );
// OR: $emails['WC_Email_Customer_Processing_Order']->trigger( $order_id );
//Pass order id from hook or function with $order_id
$order = new WC_Order( $order_id );

$note = 'Add note here';
$order->add_order_note($note);
$order->save();

I'm constructing a new class of order.我正在构建一个新的秩序。 Passing the order ID and order note and then saving the order again.传递订单 ID 和订单备注,然后再次保存订单。

This is how we update our site from our ERP.这就是我们从 ERP 更新网站的方式。 But as Loic said this method creates a private note.但正如 Loic 所说,这种方法会创建一个私人笔记。 Use his用他的

$order->set_customer_note($note);

to create a customer note.创建客户备注。

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

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