[英]No hook for new order on woocommerce in a plugin
我嘗試了大約 5 個鈎子來獲得完成的訂單鈎子,並且 function 根本沒有運行,但 woocommerce_add_to_cart 例如正在工作!
1. woocommerce_order_status_changed
2. woocommerce_new_order
我只是提醒知道 function 是否運行,但功能非常大,我在 index.ZE1BFD762321E496CEEZAC48 中的插件中部署此index.php
中的插件之前進行了命令manullay作為測試
function SP_order_token($order_id)
{
?>
<script>
alert("hello");
alert('<?php echo $order_id; ?>');
</script>
<?php
echo "hello";
global $woocommerce, $post;
echo $order_id;
$order = wc_get_order($order_id);
$order_data = $order->get_data(); // The Order data
var_dump($order_data);
}
// the final hook is when an order successfully paid
add_action('woocommerce_order_status_changed', 'SP_order_token',10,1);
請用這個替換並再次檢查。
function woo_order_status_change_custom_check($order_id) {
?>
<script>
alert("hello");
alert('<?php echo $order_id; ?>');
</script>
<?php
// echo "hello";
$order = new WC_Order( $order_id );
$orderstatus = $order->status;
var_dump($orderstatus);
}
add_action('woocommerce_order_status_changed', 'woo_order_status_change_custom_check', 10, 1);
您調試 php 的方法是錯誤的。 你不能在服務器端提醒或做 JS 的事情。 此外, var_dump
和echo
可以工作,但你不知道他們會在哪里回顯或轉儲 output。
php 調試的正確方法是將 output 寫入外部文件或錯誤日志中。 但我更喜歡在 wordpress 的根目錄中寫入文件。
這是您可以使用的代碼片段:
function sp_order_token( $order_id ) {
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
/**
* We're using file_put_contents to create a debug.txt file in the root of wordpress where your wp-config.php exists.
*
* we're also passing true in print_r so that print_r returns the output instead of printing it during code execution.
*
* ABSPATH is a constant of wordpress root defined by core WordPress.
*/
file_put_contents( ABSPATH . 'debug.txt', print_r($order_data, true ) );
}
add_action( 'woocommerce_order_status_changed', 'sp_order_token', 10, 1 );
一旦您的操作將在 wordpress 根目錄中運行,您將找到帶有預期debug.txt
的 debug.txt。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.