简体   繁体   English

当 WooCommerce 订单进入处理状态时触发 javascript function

[英]Trigger a javascript function when a WooCommerce order comes in processing status

I am having trouble calling a js function after I process a Woocommerce order.在处理 Woocommerce 订单后,我无法调用 js function。 I have the following code in my functions.php :我的functions.php中有以下代码。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.我想调用custom.js文件中的一个js function,但到目前为止我还没有成功。 I was able to call a function from that file from an unrelated page template, but not using the woocommerce_order_status_processing call.我能够从不相关的页面模板中的该文件中调用 function,但不使用woocommerce_order_status_processing调用。 Is this possible?这可能吗?

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:在 WooCommerce 中,当客户在付款后下订单时,它会被重定向到“收到订单”(谢谢)页面,这是您唯一可以触发 javascript function 为“处理订单状态”的时刻:

1). 1)。 Enqueuing a Js File将 Js 文件排队

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). 2)。 Calling a javascript function调用 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…例如,跟踪脚本会使用这种示例,例如 Google Analytics(分析)……

You can use the “wp_footer” action in PHP to output a JS function declaration like so:您可以使用 PHP 到 output JS function 声明中的“wp_footer”操作,如下所示:

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

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

refer this site - Woocommerce checkout page hooks参考这个网站 - Woocommerce 结帐页面挂钩

example-例子-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当MathJax中的“Processing Math”达到100%时如何触发javascript函数 - How to trigger a javascript function when “Processing Math” in MathJax reaches 100% WooCommerce 挂钩(woocommerce_order_status_changed)无法运行 javascript - WooCommerce Hook ( woocommerce_order_status_changed ) cant run javascript JavaScript 订单列表处理 - JavaScript Order List Processing HTML 按钮按下 - 使用 Javascript 检查状态(单击时不触发) - HTML Button pressed - check status (not trigger when clicked) with Javascript $ http请求。 即使状态码为401,响应也会成功执行 - $http request. Response comes to success function even when status code is 401 在 woocommerce_new_order 钩子内编写的 Javascript 不工作在 function.php 中 - Javascript not working written inside woocommerce woocommerce_new_order hook inside function.php 带有类的div出现时触发函数-但不适用于该类的所有div - Trigger function when div with class comes into view - but don't apply to all divs with that class Woocommerce 触发结账页面的“订单审核”更新 - Woocommerce trigger the "order review" update in checkout page 禁用按钮时触发javascript功能? - Trigger javascript function when a button is disabled? 仅在“启用”复选框选中时触发javascript函数 - Only trigger a javascript function when checkbox is checked “on”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM