wordpresswoocommercecheckoutadd-filter

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? :)


Solution

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