I have a specific requirement to update a woocommerce order note date, please reference below image to see what i would like to change.
The problem im facing is the below code will crash my server causing internal error on localhost staging environment. The note is saved but the date is incorrect. Woocommerce saves the order notes as comments and the date is actually in the comment database table, im unable to get the below code to function, im hoping a professional can help me point out where im going wrong and how i can achieve a functional code to implement in a plugin.
Thank you in advance, been pulling my hair out and need guidance here.
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 10, 2 );
function woocommerce_process_shop_order ( $order_id ) {
//Get this order id dynamically
$order = wc_get_order( $order_id );
// The text for the note
$note = __("Custom Order Note Here");
$note_date = date('d.m.Y',strtotime("-1 days"));
// Add the note
$order->add_order_note( $note );
$order->wp_insert_comment($note_date);
// Save the data
$order->save();
}
I have a specific requirement to update a woocommerce order note date, please reference below image to see what i would like to change.
The problem im facing is the below code will crash my server causing internal error on localhost staging environment. The note is saved but the date is incorrect. Woocommerce saves the order notes as comments and the date is actually in the comment database table, im unable to get the below code to function, im hoping a professional can help me point out where im going wrong and how i can achieve a functional code to implement in a plugin.
Thank you in advance, been pulling my hair out and need guidance here.
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 10, 2 );
function woocommerce_process_shop_order ( $order_id ) {
//Get this order id dynamically
$order = wc_get_order( $order_id );
// The text for the note
$note = __("Custom Order Note Here");
$note_date = date('d.m.Y',strtotime("-1 days"));
// Add the note
$order->add_order_note( $note );
$order->wp_insert_comment($note_date);
// Save the data
$order->save();
}
With the method add_order_note you can add an order note to you order. If you look into the code of this method, you will see that this method is only a wrapper around the wp_insert_comment function from WordPress.
So your problem is, that you want to change the date of the order note. But that is not possible with the method add_order_note. If you look back into the code of that method you will see, that the method has only the arguments "$note", "$is_customer_note" and "$added_by_user". So no date.
The easiest way would be to save the order note not with "add_order_note" but with the function "wp_insert_comment".
$test_date = '2005-08-05 10:41:13';
$note = __("Custom Order Note Here");
$user = get_user_by( 'id', get_current_user_id() );
$data = array(
'comment_post_ID' => $order_id,
'comment_author' => $user->display_name,
'comment_author_email' => $user->user_email,
'comment_author_url' => '',
'comment_content' => $note,
'comment_agent' => 'WooCommerce',
'comment_type' => 'order_note',
'comment_parent' => 0,
'comment_approved' => 1,
'comment_date' => $test_date,
);
wp_insert_comment($data);
// Save the data $order->save();
not mean save the order note? Any help is greatly appreciated – wayne Commented Jul 20, 2018 at 4:06