Ecommerce Shopify WordPress Discussion

I want to change status based on order notes

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 :' ) !== 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

TurboCommerce make the better internet purchasing globaly

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

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.