WooCommerce自定义下订单验证

时间:2016-01-29 10:00:13

标签: wordpress woocommerce checkout add-filter

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

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

2 个答案:

答案 0 :(得分:4)

尝试这样的事情..

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

答案 1 :(得分:0)

你可以试试专用的动作钩子:

<块引用>

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;

}