WooCommerce custom place order validation

I would like to do som additional checks when the user has submitted an order in Woocommerce. Have tried using different add_filter methods, but it seems that I can’t hook up to a place_order filter. I have tried with:

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
  }
}

But non of above is being triggered when I submit the checkout form.
Any suggestions? 🙂

Related posts

2 comments

  1. try something like this..

    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 is an array like this..

    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] => 
    )
    
  2. You can try the dedicated action hook:

    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;
    

    }

Comments are closed.