WooCommerce 自定义下单验证

WooCommerce custom place order validation

当用户在 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
  }
}

但是当我提交结账表格时,没有触发上述任何一种情况。 有什么建议么? :)

试试这样的东西..

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' );
    }

}

$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] => 
)

您可以试试专用动作挂钩:

woocommerce_checkout_process

add_action('woocommerce_checkout_process', 'validate_course_enrolled');

function validate_course_enrolled(){
$cart_items_count = WC()->cart->get_cart_contents_count();
$total_count = $cart_items_count + $quantity;

if( $cart_items_count >= 2 ){
    // Set to false
    $passed = false;
    // Display a message
     wc_add_notice( __( "You can’t buy more than one course", "woocommerce" ), "error" );
}
return $passed;

}