WooCommerce custom place order validation WooCommerce custom place order validation wordpress wordpress

WooCommerce custom place order validation


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


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;

}