简体   繁体   English

WooCommerce 挂钩(woocommerce_order_status_changed)无法运行 javascript

[英]WooCommerce Hook ( woocommerce_order_status_changed ) cant run javascript

i tried to run javascript in this hook, but it cant work.我试图在这个钩子中运行 javascript,但它不能工作。 I am pretty sure that this hook is triggered as i can perform php insert to the database and write to debug_log.我很确定这个钩子被触发了,因为我可以执行 php 插入数据库并写入 debug_log。 I would like to ask does it have the solution to solve it?请问有没有办法解决呢?

function.php function.php

add_action( 'woocommerce_order_status_changed', 'your_function', 10, 3 ); 
function your_function( $order_id, $old_status, $new_status ){ 

    // This doesnt work

    // Enqueued script with localized data.
    wp_enqueue_script( 'order_status_changed_1', 'https://www.gstatic.com/firebasejs/7.3.0/firebase-app.js');
    wp_enqueue_script( 'order_status_changed_2', 'https://www.gstatic.com/firebasejs/7.3.0/firebase-auth.js');
    wp_enqueue_script( 'order_status_changed_3', 'https://www.gstatic.com/firebasejs/7.3.0/firebase-firestore.js');
    wp_enqueue_script( 'order_status_changed_4', plugin_dir_url( __FILE__ ) . 'js/scripts_v1.js' );

    // Localize the script with new data
    $data = array(
        'order_number' => $order_id,
        'order_status' => $new_status,
        'action' => 'update_order_status'
    );
    wp_localize_script( 'order_status_changed_4', 'data', $data );

    // This work (Below code)

    global $wpdb;
    $sql = $wpdb->prepare("INSERT INTO `test`(`order_id`, `status`) VALUES (%s,%s)",$order_id,$new_status);
    $wpdb->query($sql);

    error_log("Trigger");
}

scripts_v1.js scripts_v1.js

var action = data.action;
if(action == "update_order_status" ){

    console.log("here");

    db.collection("orders").doc(data.order_number).update({
        orderStatus : data.order_status
    })
    .then(function() {
        console.log("Document successfully updated!");
    })
    .catch(function(error) {
        // The document probably doesn't exist.
        console.error("Error updating document: ", error);
    });
}

The hook might be triggered but that's not the way to enqueue scripts.钩子可能会被触发,但这不是将脚本排入队列的方式。 Scripts are added by:脚本通过以下方式添加:

add_action( 'admin_enqueue_scripts', 'your_function')
function your_function(){
    wp_enqueue_script( 'order_status_changed_1','https://www.gstatic.com/firebasejs/7.3.0/firebase-app.js');
}

That means your JS is not added to the page (check your Network tab in developer tools).这意味着您的 JS 未添加到页面中(检查开发人员工具中的网络选项卡)。

You could add the scripts in a different way by outputting the script directly.您可以通过直接输出脚本以不同的方式添加脚本。 Or you conditionally load your scripts on the page that is loaded after the order status is changed.或者您有条件地在订单状态更改后加载的页面上加载脚本。

That means your use the enqueue action as usual.这意味着您像往常一样使用入队操作。 But you add a condition for enqueuing the script:但是您添加了一个使脚本入队的条件:

if(is_page('order-status-changed'){
// wp_enqueue_script
}

Maybe you can give some more info surrounding the order change status, and what your objective is?也许您可以提供更多有关订单更改状态的信息,以及您的目标是什么?

Hope help:希望帮助:

if(data.action == "update_order_status" ){ <-- edit

    console.log("here");

    db.collection("orders").doc(data.order_number).update({
        orderStatus : data.order_status
    })
    .then(function() {
        console.log("Document successfully updated!");
    })
    .catch(function(error) {
        // The document probably doesn't exist.
        console.error("Error updating document: ", error);
    });
}

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

相关问题 在 woocommerce_new_order 钩子内编写的 Javascript 不工作在 function.php 中 - Javascript not working written inside woocommerce woocommerce_new_order hook inside function.php 当 WooCommerce 订单进入处理状态时触发 javascript function - Trigger a javascript function when a WooCommerce order comes in processing status 在收到的Woocommerce订单中的javascript中设置订单ID和订单总数 - Set order id and order total in a javascript on Woocommerce order received woocommerce订单数量倍数 - woocommerce order quantity multiples Javascript - 用 Woocommerce 订单表数据填写 PDF 上的 HTML 表 - Javascript - Fill HTML table on PDF with Woocommerce order table data 使用自定义Woocommerce付款网关对“下订单按钮”进行JavaScript覆盖 - Javascript override for “Place Order button” using custom Woocommerce payment gateway 在 WooCommerce 的 HEAD 中包含一些 javascript 代码收到订单 - Include some javascript code in HEAD on WooCommerce Order received 如果选择了特定的付款方式,Woocommerce只能运行javascript - Woocommerce to only run javascript if a specific payment method is selected 在 ajax 添加到 Woocommerce 中的购物车事件后运行 javascript 代码 - Run javascript code after ajax add to cart event in Woocommerce 覆盖WooCommerce前端Javascript - Override WooCommerce Frontend Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM