Docs

Trigger an Automation for Past WooCommerce Orders

Updated on

In this step-by-step document, we’ll help you trigger an automation for past WooCommerce orders by adding bulk order notes to WooCommerce orders.

Follow these simple instructions to automate your workflow efficiently.

Step 1: Add a code snippet

Use the WP Code or Code Snippets plugin to add the snippet to your website.

Go to Snippets from the WordPress menu and copy/paste this snippet into it.

/*
To execute the snippet hit the URL : https://[enter_domain_name_here]/?added 
For example, https://google.com/?added
*/

function add_private_note_to_all_orders() {
    // Only run when '?added' is present in the URL
    if ( ! isset( $_GET['added'] ) ) {
       return;
    }

    // Fetch all WooCommerce orders
    $orders = wc_get_orders([
       'limit'  => -1,
       'status' => 'any',
       'return' => 'ids',
    ]);

    if ( empty( $orders ) ) {
       echo "No orders found.";
       return;
    }

    $note_content = 'test1';// here you can add note text

    foreach ( $orders as $order_id ) {
       $order = wc_get_order( $order_id );

       if ( ! $order ) {
          continue;
       }

       // Check if the note already exists
       $note_exists = array_filter(
          wc_get_order_notes([ 'order_id' => $order_id ]),
          fn( $note ) => strpos( $note->content, $note_content ) !== false
       );

       if ( $note_exists ) {
          echo "Private note already exists for order #$order_id. Skipping.<br>";
          continue;
       }

       // Prepare and filter note data
       $note_data = apply_filters( 'woocommerce_new_order_note_data', [
          'comment_post_ID' => $order_id,
          'comment_author'  => 'Admin',
          'comment_content' => $note_content,
          'comment_type'    => 'order_note',
          'comment_approved'=> 1,
       ], [
          'order_id'         => $order_id,
          'comment_content'  => $note_content,
          'is_customer_note' => false,
       ]);

       // Add the private note
       $comment_id = wp_insert_comment( $note_data );

       if ( $comment_id ) {
          update_comment_meta( $comment_id, 'is_customer_note', 0 ); // Mark as private
          echo "Private note '$note_content' added to order #$order_id.<br>";
       } else {
          echo "Error: Unable to add the private note for order #$order_id.<br>";
       }
    }

    echo "All orders have been processed.";
}

// Run the function once when visiting an admin page
add_action( 'wp', 'add_private_note_to_all_orders' );

Add a new snippet and paste this snippet:

Add snippet screen for a custom code snippet that triggers past WooCommerce orders

Activate it to run on your website.

Activate it to run on your website

Please note that test1 ($note_content = ’test1’; in the code) will be added as order notes in all WooCommerce orders to trigger automation for past orders. 

Step 2: Set up an automation

If you’re new to FunnelKit Automations, set up your first automation here.

Next, select the Order Note Added event and configure your automation trigger.

Order note mode option selected when setting up an automation

Click on Save once done.

Specify any action which you want to trigger automations for past WooCommerce orders.

We’ll simply add the Debug action here.

Once you’ve created your entire workflow, activate this automation.

Order note automation configured to trigger on a private note

Step 3: Trigger the automation

Enter your URL: https://your-domain.com/?added 

Private note added to a WooCommerce order to trigger the automation

This will add the order notes to all your WooCommerce orders as you can see below:

Edit order screen in WooCommerce with the order notes panel

Wait for some time, and your automation with respective actions will run successfully.

Wait for some time, and your automation with respective actions will run successfully

This is how you can trigger an automation for past WooCommerce orders with FunnelKit Automations.