WooCommerce 自定义下单验证

sim*_*mon 4 wordpress checkout add-filter woocommerce

当用户在 Woocommerce 中提交订单时,我想做一些额外的检查。尝试使用不同的 add_filter 方法,但似乎我无法连接到 place_order 过滤器。我试过:

add_filter('woocommerce_place_order', 'checkFields'); //on ordersubmit
function checkFields(){
  if($this != true){
    //not allowed to place order because of data is not true
  }else{
   //continue order
  }
}
Run Code Online (Sandbox Code Playgroud)

但是当我提交结帐表格时,不会触发上述情况。有什么建议?:)

Rei*_*gel 5

尝试这样的事情..

add_action('woocommerce_after_checkout_validation', 'rei_after_checkout_validation');

function rei_after_checkout_validation( $posted ) {

    // do all your logics here...
    // adding wc_add_notice with a second parameter of "error" will stop the form...
    // wc_add_notice( __( "OMG! You're not human!", 'woocommerce' ), 'error' );

    if (empty($_POST['captcha'])) {
         wc_add_notice( __( "Captcha is empty!", 'woocommerce' ), 'error' );
    }

}
Run Code Online (Sandbox Code Playgroud)

$posted 是这样的数组..

Array
(
    [terms] => 0
    [createaccount] => 0
    [payment_method] => cheque
    [shipping_method] => 
    [ship_to_different_address] => 
    [billing_first_name] => 
    [billing_last_name] => 
    [billing_company] => 
    [billing_email] => iamhuman@gmail.com
    [billing_phone] => 
    [billing_country] => PH
    [billing_address_1] => 
    [billing_address_2] => 
    [billing_city] => 
    [billing_state] => 
    [billing_postcode] => 
    [order_comments] => 
)
Run Code Online (Sandbox Code Playgroud)