问题
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