亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當 WooCommerce 訂單進入處理狀態時觸發 javascript 函數

當 WooCommerce 訂單進入處理狀態時觸發 javascript 函數

PHP
嗶嗶one 2023-09-15 18:22:32
我在處理 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    }我想調用文件中的js函數custom.js,但到目前為止我還沒有成功。我能夠從不相關的頁面模板調用該文件中的函數,但不使用該woocommerce_order_status_processing調用。這可能嗎?
查看完整描述

3 回答

?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

在 WooCommerce 中,當客戶付款后下訂單時,它會被重定向到“訂單已收到”(謝謝)頁面,這是您可以觸發 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……


查看完整回答
反對 回復 2023-09-15
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

您可以使用 PHP 中的“wp_footer”操作來輸出 JS 函數聲明,如下所示:


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/


查看完整回答
反對 回復 2023-09-15
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

例子-


?function ts_review_order_before_submit(){

? ?echo'<button onclick="js_function()" id="function" > JS?

? ? ? ? ? function?

? ? ? ? </button>'

?}


查看完整回答
反對 回復 2023-09-15
  • 3 回答
  • 0 關注
  • 182 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號