Created
March 11, 2018 15:15
-
-
Save woogists/d8d6845db93eb35b36102f4fd9ed8202 to your computer and use it in GitHub Desktop.
[Customizing checkout fields using actions and filters] Validate WooCommerce custom checkout field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/