I want to change order status processing to completed if order notes contains "SHIPMENT NUMBER" then change order status based on content.
I tried below code for getting all orders and all order's notes and scan notes content and detect "SHIPMENT NUMBER".
So below code will works when admin add any manual notes and notes contain "SHIPMENT NUMBER" than it will work. But I want to change based on programmatically added notes.
`
// Hook into the wp_loaded action to trigger the update when WordPress loads
add_action( 'woocommerce_order_note_added', 'update_order_status_based_on_delhivery_strings', 10, 2);
// Function to update order status based on Delhivery strings
function update_order_status_based_on_delhivery_strings() {
// Get all processing orders
$orders = wc_get_orders( array( 'status' => 'processing', 'limit' => -1 ) );
foreach ( $orders as $order ) {
// Get order ID
$order_id = $order->get_id();
// Get all order notes
$order_notes = wc_get_order_notes( array(
'order_id' => $order_id,
'orderby' => 'date_created_gmt',
'type' => 'internal'
));
foreach ( $order_notes as $note ) {
$note_content = $note->content;
// Check if the note contains specific Delhivery strings indicative of an update
$delhivery_strings_present = (
stripos( $note_content, 'SHIPMENT NUMBER :' ) !== false &&
stripos( $note_content, 'SHIPPER : Delhivery' ) !== false &&
stripos( $note_content, 'TRACKING : https://www.delhivery.com/track/package/' ) !== false
);
if ( $delhivery_strings_present ) {
// Log a message for debugging purposes
error_log( 'Updating order status to completed for Order ID: ' . $order_id );
// Update the order status to "completed"
$order->update_status( 'completed' );
break; // Exit the loop after updating the status for efficiency
}
}
}
}
`
First you dont need to loop all orders since woocommerce_order_note_added returns order object. Also you look for SHIPMENT NUMBER : not SHIPMENT NUMBER
add_action( 'woocommerce_order_note_added', 'update_order_status_based_on_delhivery_strings', 10, 2 );
function update_order_status_based_on_delhivery_strings( $note_id, $order ) {
// Get the note content
$note = get_comment( $note_id );
$note_content = $note->comment_content;
// Check if the note contains 'SHIPMENT NUMBER'
if ( false !== stripos( $note_content, 'SHIPMENT NUMBER' ) ) {
// Change the order status to the desired status and add note why status is changed
$order->update_status( 'completed', __( 'Order status changed based on SHIPMENT NUMBER note.', 'your-text-domain' ) );
}
}
January 20, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
January 20, 2024