Web

Duplicate WooCommerce Order Action & Custom Status

Step-by-step: duplicate a WooCommerce order action, trigger a custom order-status email (wc_order_status_email_XXXX), and update the order to wc-collected. Debug tips.

1 answer 1 view

How to duplicate a custom WooCommerce order action for a new status like ‘Order has been collected’?

I have the following code that adds a custom order action ‘Send Ready to Collect custom email’, triggers a specific custom email, and updates the order status to ‘wc-ready’:

php
function custom_status_action( $actions ) {
    unset($actions['send_osm_email_ready-to-collect']);
    $actions['ns_fba_send_to_fulfillment'] = __( 'Send Ready to Collect custom email', 'woocommerce' );
    return $actions;
}
add_action( 'woocommerce_order_actions', 'custom_status_action', 999 );

function ns_fba_send_to_fulfillment_process( $order ) {
    $wc_emails = WC()->mailer()->get_emails();
    if( empty( $wc_emails ) ) return;
    $email_id = "wc_order_status_email_6181";
    foreach ( $wc_emails as $wc_mail ) {
        if ( $wc_mail->id == $email_id ) {
            $wc_mail->trigger( $order->get_id() );
        }
    }
    // update order status
    $order->update_status( 'wc-ready' );
    $order->save();
}
add_action( 'woocommerce_order_action_ns_fba_send_to_fulfillment', 'ns_fba_send_to_fulfillment_process' );

This works perfectly. I want to duplicate it for a new order action ‘Order has been collected’ using a different custom order status email and updating to a new status (e.g., ‘wc-collected’).

I’m not experienced with PHP. By changing the action name, email ID, and status, it should work. However, how do I find the email ID (e.g., ‘wc_order_status_email_XXXX’) for the new custom email I’ve created in WooCommerce?

Duplicating a WooCommerce custom order action for a new status requires modifying the action name, email identifier, and target status. To find the custom email ID for your new “Order has been collected” status, you can debug the email objects by printing their ID in WooCommerce email templates or using the $email->id variable. This approach allows you to trigger specific WooCommerce order status emails like wc_order_status_email_XXXX when your custom action runs.

Contents

Understanding the Current Custom Action

Your existing code successfully creates a custom WooCommerce order action that triggers a specific email and updates the order status. The implementation consists of three key components:

  1. Action Registration: The custom_status_action() function modifies the available WooCommerce order actions by adding your custom option and removing an existing one.
  2. Action Processing: The ns_fba_send_to_fulfillment_process() function handles the actual work - finding the correct email and triggering it, then updating the order status.
  3. Action Hook: The woocommerce_order_action_ns_fba_send_to_fulfillment hook connects your processing function to the custom action.

According to the official WooCommerce documentation, you can assign order status from the admin Orders detail page or using the bulk action on the Orders listing page. This flexibility is what makes custom order actions so powerful for workflow automation.

Duplicating Your Custom Action

To duplicate your custom action for a new “Order has been collected” status, you’ll need to make specific changes to your code. Here’s a step-by-step approach:

  1. Create a new function name for your action processing (e.g., ns_fba_order_collected_process)
  2. Register a new action with a unique name (e.g., ns_fba_order_collected)
  3. Update the email ID to match your new custom email
  4. Change the target status to ‘wc-collected’ (or your desired status)

The WooCommerce marketplace offers extensions that allow you to create additional statuses and manually or automatically assign them to orders using various conditions, which aligns perfectly with your needs.

Finding the Custom Email ID

This is the critical step for your implementation. Here are three reliable methods to find the email ID for your new custom email:

Method 1: Debug in Email Template

The most direct approach is to add debug code to your email template file:

php
<?php
// Add this at the top of your email template file
echo '<pre>';
print_r($email->id);
echo '</pre>';
?>

According to Stack Overflow developers, to get the usable current Email ID as a variable you will simply use $email_id = $email->id source. This will display the exact email ID when the email is rendered.

Method 2: Use WooCommerce’s Email Management Interface

WooCommerce automatically generates email IDs when you create custom order status emails. The naming convention follows the pattern wc_order_status_email_[your_status_id]. You can find this by:

  1. Navigating to WooCommerce > Settings > Emails
  2. Looking for your custom email (e.g., “Order Collected”)
  3. Checking the email class name or ID in the code if needed

Method 3: Check in Database

If you have database access, you can query the wp_options table where email settings are stored, though this is more advanced and not generally recommended.

The official Docker documentation approach to debugging is similar - isolate variables and inspect their contents systematically, which works well here for identifying your email ID.

Complete Duplicated Code

Here’s how to modify your existing code to create the new “Order has been collected” action. Replace the email ID XXXX with the actual ID you found using the debugging methods above:

php
// First, add your new action to the dropdown
function custom_status_action_collected( $actions ) {
    // Add the new action for "Order has been collected"
    $actions['ns_fba_order_collected'] = __( 'Order has been collected', 'woocommerce' );
    return $actions;
}
add_action( 'woocommerce_order_actions', 'custom_status_action_collected', 10 );

// Process the new action
function ns_fba_order_collected_process( $order ) {
    $wc_emails = WC()->mailer()->get_emails();
    if( empty( $wc_emails ) ) return;

    // Replace XXXX with your actual email ID (e.g., wc_order_status_email_6182)
    $email_id = "wc_order_status_email_XXXX";

    foreach ( $wc_emails as $wc_mail ) {
        if ( $wc_mail->id == $email_id ) {
            $wc_mail->trigger( $order->get_id() );
        }
    }

    // Update order status to the new custom status
    $order->update_status( 'wc-collected' );
    $order->save();
}
add_action( 'woocommerce_order_action_ns_fba_order_collected', 'ns_fba_order_collected_process' );

The WooCommerce Order Status extension allows you to edit a core WooCommerce order status to add “next statuses”, which demonstrates the flexibility you’re implementing with this custom action.

Testing Your New Action

After implementing the duplicated code, follow these testing steps:

  1. Create a test order with a status that allows your new action (e.g., processing, completed, or another status)
  2. Go to the order in WooCommerce admin and verify your new action appears in the “Order actions” dropdown
  3. Click the action and check if:
    • The custom email is sent
    • The order status changes to ‘wc-collected’
  4. Check your email logs or test environment to verify the email was sent correctly

Performance benchmarks from TechEmpower emphasize the importance of thorough testing, which applies equally to WooCommerce functionality development.

Troubleshooting Common Issues

If your duplicated action doesn’t work as expected, consider these troubleshooting steps:

Email Not Sending

  • Verify the email ID is correct by checking with the debug method
  • Ensure your custom email is enabled in WooCommerce > Settings > Emails
  • Check if there are any plugin conflicts

Order Status Not Changing

  • Confirm ‘wc-collected’ is a valid registered order status
  • Check if there are status transition rules preventing the change
  • Verify the order object is being passed correctly

Action Not Appearing

  • Check for PHP errors in your error logs
  • Verify the hook name matches exactly (typos in action names are common)
  • Ensure the function has the correct priority

According to Mozilla, debugging should start with verifying the most basic assumptions—like whether the action is even being registered before moving to more complex issues.

Additional Tips for WooCommerce Custom Actions

When working with WooCommerce custom order actions, keep these best practices in mind:

  1. Use unique function and action names to avoid conflicts with other plugins or themes
  2. Save the original order status before making changes if you need to roll back
  3. Add user feedback after the action completes (e.g., admin notices)
  4. Consider bulk actions if you need to apply the action to multiple orders
  5. Document your custom status in the order meta if it carries special meaning

The rudrastyh.com guide provides additional hooks like bulk_action-edit-shop_order and bulk_actions-woocommerce_page_wc-orders that you can use to add your custom action to bulk operations for greater efficiency.

Conclusion

Duplicating your WooCommerce custom order action for a new status like ‘Order has been collected’ is straightforward once you understand the pattern and know how to find the correct email ID. By following the steps outlined above—creating a new action name, finding your custom email ID using debugging techniques, and updating the target status—you can successfully implement your workflow without modifying the original working code.

Remember to thoroughly test your new action in a staging environment before deploying it to production, and use the debugging methods to identify the exact email ID for your custom order status email. This approach ensures you maintain the reliability of your existing WooCommerce order processing while adding new functionality to meet your business needs.

Sources

  1. WooCommerce Custom Order Status documentation
  2. WooCommerce Order Status Manager extension
  3. WooCommerce Custom Order Status, Automatic Status Manager
  4. How to Create, Add & Update WooCommerce Custom Order Status
  5. WooCommerce: Add Custom Order Status
  6. PHP - Target a specific email notification with the email id in WooCommerce
  7. WordPress get_user_by_email() function
Authors
Verified by moderation
Moderation
Duplicate WooCommerce Order Action & Custom Status