In WooCommerce, When customer place an order after payment, it's redirected to "Order received" (Thankyou) page, that is the only moment where you can trigger javascript function for a "processing" order status:
1). Enqueuing a Js File
add_action('wp_enqueue_scripts', 'order_received_enqueue_js_script');
function order_received_enqueue_js_script() {
// Only on order received" (thankyou)
if( ! is_wc_endpoint_url('order-received') )
return;
$order_id = absint( get_query_var('order-received') ); // Get the order ID
$order = wc_get_order( $order_id ); // Get the WC_Order Object
// Only for processing orders
if ( ! is_a( $order, 'WC_Order') || ! $order->has_status( 'processing' ) ) {
return;
}
wp_enqueue_script(
'my-java', // Handle
get_stylesheet_directory_uri() . '/js/custom.js?V=2020.08.01v12', // URL for child or parent themes
array( 'jquery' ), // Dependencies
false, // Version
true // In footer
);
}
2). Calling a javascript function
add_action('wp_footer', 'order_received_js_script');
function order_received_js_script() {
// Only on order received" (thankyou)
if( ! is_wc_endpoint_url('order-received') )
return; // Exit
$order_id = absint( get_query_var('order-received') ); // Get the order ID
if( get_post_type( $order_id ) !== 'shop_order' ) {
return; // Exit
}
$order = wc_get_order( $order_id ); // Get the WC_Order Object
// Only for processing orders
if ( method_exists( $order, 'has_status') && ! $order->has_status( 'processing' ) ) {
return; // Exit
}
?>
<script>
// Once DOM is loaded
jQuery( function($) {
// Trigger a function (example)
myJsFunctionName('order-received', 'processing', {
'order_id': '<?php echo $order->get_order_id(); ?>',
'order_key': '<?php echo $order->get_order_key(); ?>',
'transaction_id': '<?php echo $order->get_order_id(); ?>',
'total': '<?php echo $order->get_total(); ?>',
'currency': '<?php echo $order->get_currency(); ?>'
});
});
</script>
<?php
}
This kind of example is used by tracking scripts for example, like for GooGle Analytics…