Skip to content

Instantly share code, notes, and snippets.

@woogists
Created March 11, 2018 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save woogists/d8d6845db93eb35b36102f4fd9ed8202 to your computer and use it in GitHub Desktop.
Save woogists/d8d6845db93eb35b36102f4fd9ed8202 to your computer and use it in GitHub Desktop.
[Customizing checkout fields using actions and filters] Validate WooCommerce custom checkout field
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
@BruceIvar
Copy link

According to the woocommerce page: "A checkout error is displayed if the field is blank". But the label of "My Field" has the word "Optional", when I follow the steps there and this code. If it cannot left blank, then it is not "optional", as the label says. So this is confusing for the shopper.

https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment