I want to create a code snippet that completes a Woocommerce order based on a specific order note (“Budbee order status: Delivered”). I am using a hook to do this and I have some code but it doesn’t work. Can anyone please help me? PHP is not my “native” language, so I am not sure if and where I made mistakes.
Two main questions are:
- Am I using the right hook
woocommerce_new_customer_note
? - Is my PHP code correct?
Below is the code:
add_action( 'woocommerce_new_customer_note', 'auto_complete_budbee_delivered' );
function auto_complete_budbee_delivered( $order_id, $customer_note ) {
if ($customer_note == "Budbee order status: Delivered") {
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
}
}
Thanks a million!!
UPDATE
I think I choose the wrong hook (customer note, instead of order note). It might should be:
/**
* Action hook fired after an order note is added.
*
* @param int $order_note_id Order note ID.
* @param WC_Order $order Order data.
*
* @since 4.4.0
*/
do_action( 'woocommerce_order_note_added', $comment_id, $this );
return $comment_id;
The problem here is that you only get the comment_id back, and not the message or the order_id. Any edea how it would work to obtain the order_id and content of the note? This is from the same doc that was shared by @LuicTheAztec. Is $this
the object containing all the order info?
2
Answers
Updated (removed wrong
$
from array key in$args['customer_note']
).You have not set the function arguments in the correct way for
woocommerce_new_customer_note
(see it in the hook source code), so try the following instead:This should better work now.
Edit (related to your question edit).
Or you may be better use instead
woocommerce_order_note_added
hook this way:This could works if the note content is exactly "Budbee order status: Delivered".
For contain word
// order complete note contain strings