javascript - 当 WooCommerce 订单进入处理状态时触发 javascript 函数

标签 javascript php jquery wordpress woocommerce

我在处理 Woocommerce 订单后调用 js 函数时遇到问题。我的 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
    }

我想调用 custom.js 文件中的 js 函数,但到目前为止我还没有成功。我能够从不相关的页面模板调用该文件的函数,但不能使用 woocommerce_order_status_processing 调用。这可能吗?

最佳答案

在 WooCommerce 中,当客户在付款后下订单时,它会被重定向到“已收到订单”(Thankyou) 页面,这是您唯一可以触发 javascript 函数以获取“正在处理”订单状态的时刻:

1).入队一个 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).调用 javascript 函数

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
}

这种示例被跟踪脚本使用,例如用于 GooGle Analytics ......

关于javascript - 当 WooCommerce 订单进入处理状态时触发 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63273212/

相关文章:

javascript - 根据滚动位置淡入淡出图像

jquery - jqGrid - 如何将网格设置为最初不加载任何数据?

javascript - 如何填补 ASP.NET Webform 中服务器端和客户端之间的空白?

php - 下载 Excel 文件以响应提交表单

php - 菜鸟 URL 重写

javascript - ajax内容的内联缓存&lt;script&gt;

javascript - 将 s3.getObject 响应转换为 File 对象

javascript - 滚动时显示两个像素值之间的 div

javascript - 将值传递给 Symbol.iterator

PHP : how to allow only registered users to go to another page