[英]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.