Add customer note from another script in WooCommerce

拈花ヽ惹草 提交于 2021-02-05 07:09:25

问题


I have a script that changes the status of orders based on our ERP system.

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); it only updates the status.

Is there a way to add a note outside the edit screen? (Including e-mailing the customer)


回答1:


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):

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

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

$emails['WC_Email_Customer_Completed_Order']->trigger( $order_id );
// OR: $emails['WC_Email_Customer_Processing_Order']->trigger( $order_id );



回答2:


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

This is how we update our site from our ERP. But as Loic said this method creates a private note. Use his

$order->set_customer_note($note);

to create a customer note.



来源:https://stackoverflow.com/questions/57075194/add-customer-note-from-another-script-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!