1

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

}

Woocommerce Order Note Date

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

1 Answer 1

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);
3
  • Thank you so much, can i remove the first 'comment_type' => '',
    – wayne
    Commented Jul 20, 2018 at 18:07
  • Tested this now and it works great, how could i get the order id date dynamically to replace it with $test_date = '2005-08-05 10:41:13'; Thanx in advance!!
    – wayne
    Commented Jul 20, 2018 at 18:13
  • Do you mean something like that $test_date = date('Y-m-d H:i:s', strtotime("-1 days") ); ?
    – user141080
    Commented Jul 21, 2018 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.