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……

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/

TA貢獻1801條經驗 獲得超8個贊
例子-
?function ts_review_order_before_submit(){
? ?echo'<button onclick="js_function()" id="function" > JS?
? ? ? ? ? function?
? ? ? ? </button>'
?}
- 3 回答
- 0 關注
- 182 瀏覽
添加回答
舉報