最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Trigger a javascript function when a WooCommerce order comes in processing status - Stack Overflow

programmeradmin3浏览0评论

I am having trouble calling a js function after I process a Woomerce order. I have the following code in my functions.php:

add_action( 'wp_enqueue_scripts', 'enqueue_so_18552010' );
add_action( 'woomerce_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 woomerce_order_status_processing call. Is this possible?

I am having trouble calling a js function after I process a Woomerce order. I have the following code in my functions.php:

add_action( 'wp_enqueue_scripts', 'enqueue_so_18552010' );
add_action( 'woomerce_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 woomerce_order_status_processing call. Is this possible?

Share edited Aug 6, 2020 at 1:12 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Aug 5, 2020 at 20:43 wachingwaching 1572 silver badges9 bronze badges 1
  • 3 PHP executes code on server side, javascript code is executed on browser.I think it's impossible to execute js code using php. – Marios Nikolaou Commented Aug 5, 2020 at 20:49
Add a ment  | 

3 Answers 3

Reset to default 4

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…

You can use the “wp_footer” action in PHP to output a JS function declaration like so:

function my_php_function() {
echo '<script>
function myFunction() {
  console.log("myFunction() was called.");
}
</script>';
}
add_action('wp_footer', 'my_php_function');

https://wordpress/support/topic/calling-a-function-from-functions-php/

refer this site - Woomerce checkout page hooks

example-

 function ts_review_order_before_submit(){
   echo'<button onclick="js_function()" id="function" > JS 
          function 
        </button>'
 }
发布评论

评论列表(0)

  1. 暂无评论