Reputation: 25
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:
woocommerce_new_customer_note
?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?
Upvotes: 2
Views: 1185
Reputation: 19
For contain word
// order complete note contain strings
add_action( 'woocommerce_order_note_added', 'auto_complete_budbee_delivered', 10, 2 );
function auto_complete_budbee_delivered( $comment_id, $order ) {
$comment_obj = get_comment( $comment_id );
$customer_note = $comment_obj->comment_content;
$word = "Order complete";
$mystring = $customer_note;
if (strpos($mystring, $word) !== false) {
$order->update_status( 'completed' );
}
}
Upvotes: 0
Reputation: 253814
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:
add_action( 'woocommerce_new_customer_note', 'auto_complete_budbee_delivered' );
function auto_complete_budbee_delivered( $args ) {
if ( $args['customer_note'] == "Budbee order status: Delivered") {
$order = wc_get_order( $args['order_id'] );
$order->update_status( 'completed' );
}
}
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:
add_action( 'woocommerce_order_note_added', 'auto_complete_budbee_delivered', 10, 2 );
function auto_complete_budbee_delivered( $comment_id, $order ) {
$comment_obj = get_comment( $comment_id );
$customer_note = $comment_obj->comment_content;
if ( $customer_note == "Budbee order status: Delivered") {
$order->update_status( 'completed' );
}
}
This could works if the note content is exactly "Budbee order status: Delivered".
Upvotes: 3