我需要將訂單詳細信息發送到公司內部數據庫。如果對數據庫的調用返回成功響應,我希望只能保存訂單。如果沒有,我想停止訂單并在結帳屏幕上顯示一條消息。我正在嘗試掛鉤woocommerce_checkout_create_order,如果響應為 false,則返回 false 并停止訂購流程:add_action( 'woocommerce_checkout_create_order', 'process_new_order_details', 10, 2 ); function process_new_order_details( $order ) { ...... if($json_response->success == 1) { // We got a success message back from internal DB return $order; } else { // Could not save in internal DB return false; }但無論 web 服務響應如何,它總是將訂單保存到數據庫中。有什么想法嗎?當 Web 服務響應未“成功”顯示自定義錯誤通知時,如何避免保存訂單?
1 回答

繁華開滿天機
TA貢獻1816條經驗 獲得超4個贊
您將使用以下內容:
add_action( 'woocommerce_checkout_create_order', 'process_new_order_details', 10, 2 );
function process_new_order_details( $order, $data ) {
# ...... / ......
if($json_response->success == 1) {
return $order;
} else { // Throw an error notice
$error_text = __("My custom error notice", "wocommerce" );
throw new Exception( $error_text );
}
}
代碼進入您的活動子主題(或活動主題)的 functions.php 文件。測試和工作。
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報
0/150
提交
取消