I am having trouble calling a js function after I process a Woocommerce order. I have the following code in my functions.php
:
add_action( 'wp_enqueue_scripts', 'enqueue_so_18552010' );
add_action( 'woocommerce_order_status_processing', 'purchaseCompleted' );
function enqueue_so_18552010()
{
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
);
}
function purchaseCompleted()
{
//JS FUNCTION'S CALL GOES HERE
}
I want to call a js function that is in the custom.js
file, but so far I haven’t been successful. I was able to call a function from that file from an unrelated page template, but not using the woocommerce_order_status_processing
call. Is this possible?
3
Answers
You can use the “wp_footer” action in PHP to output a JS function declaration like so:
https://wordpress.org/support/topic/calling-a-function-from-functions-php/
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
2). Calling a javascript function
This kind of example is used by tracking scripts for example, like for GooGle Analytics…
refer this site – Woocommerce checkout page hooks
example-