Woocommerce Update Order Notes Date

admin2025-01-08  3

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();

}

Share Improve this question edited Jul 19, 2018 at 21:17 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 19, 2018 at 17:16 waynewayne 114 bronze badges 3
  • Your code is crashing because you used the Wordpress function wp_insert_comment as method of the order object. Why don't you save the order note with this function? – user141080 Commented Jul 19, 2018 at 19:02
  • Thank you for your response, does // Save the data $order->save(); not mean save the order note? Any help is greatly appreciated – wayne Commented Jul 20, 2018 at 4:06
  • No "$order->save()" means you change the order data into the database. "add_order_note" creates the comment via "wp_insert_commt". So there is no need to call the "save" method. – user141080 Commented Jul 20, 2018 at 7:33
Add a comment  | 

1 Answer 1

Reset to default 1

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);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269252a1337.html

最新回复(0)