I run an e-commerce site and need to change my checkout error messages.
Recently I have had a customer say that they made a mistake entering their credit card's expiration date, but they were confused because the error message displayed by woocommerce says "Your card's security code is incorrect." This led them to double and triple check the security code instead of the expiration date which was the real problem.
I would like to find a solution that allows me to change that error message to say, "Your card's security code or expiration date is incorrect."
I would also be okay with a solution that allows me to input a single generic error message such as, "Some information is incorrect or missing." that would be used for all payment info errors.
Somebody please help me.
I have tried implementing the following solutions from other forums into my functions.php file with no luck:
Attempt 1:
function ShowOneError( $fields, $errors ){
// if their is any validation errors
if( !empty( $errors->get_error_codes() ) ) {
// remove all of Error msg
foreach( $errors->get_error_codes() as $code ) {
$errors->remove( $code );
}
// our custom Error msg
$errors->add('validation','There is an error in filed data.');
}
}
add_action('woocommerce_after_checkout_validation','ShowOneError',999,2);
Attempt 2:
// alter the subscriptions error
function my_woocommerce_add_error( $error ) {
if( 'The generic error message' == $error ) {
$error = 'The shiny brand new error message';
}
return $error;
}
add_filter( 'woocommerce_add_error', 'my_woocommerce_add_error' );
Attempt 3:
add_action( 'woocommerce_after_checkout_validation', 'quadlayers', 9999, 2);
function quadlayers( $fields, $errors ){
// in case any validation errors
if( !empty( $errors->get_error_codes() ) ) {
// omit all existing error messages
foreach( $errors->get_error_codes() as $code ) {
$errors->remove( $code );
}
// display custom single error message
$errors->add( 'validation', 'Your Custom Message Goes Here!!!' );
}
}
Related
Am using Contact Form 7 latest version (5.6.3) on our site and when we try to add custom validation using hooks, it doesn’t work for empty field validation. The hooks work fine when we roll back the plugin version to 5.5.6.1.
Here am attaching the screenshot of the forms. this is a screenshot for version 5.6.3 https://snipboard.io/cSusk3.jpg and this is for version 5.5.6.1 https://snipboard.io/obIpS9.jpg.
Please Check the empty field value error message. Please add this code to your site and check it. The difference between the latest version and version 5.5.6.1 can be seen then.
add_filter('wpcf7_validate_text', 'custom_text_validation', 1, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation', 1, 2);
function custom_text_validation($result, $tag) {
$type = $tag->type;
$name = $tag->name;
if($name == 'Name') {
$value = $_POST['Name'];
if($value==''){
$result->invalidate($tag, "Please enter your name.");
}
}
return $result;
}
To me, it's confusing as to why your general function didn't work. I've written other $result->invalidate functions, and they all work, but testing yours did not. This is what I came up with, but what happens is that if there's other text field errors their error message won't show until the Please enter your name message is cleared. This to me doesn't seem like a big problem.
add_filter( 'wpcf7_validate_text', 'custom_text_validation', 10, 2 );
add_filter( 'wpcf7_validate_text*', 'custom_text_validation', 10, 2 );
function custom_text_validation( $result, $tag ) {
if ( 'Name' === $tag->name && empty( $_POST['Name'] ) ) {
$result = new WPCF7_Validation();
$result->invalidate( $tag, __( 'Please enter your name.' ) );
}
return $result;
}
I would like to edit the text on the checkout page. There are some issues with the items in your cart. Please go back to the cart page and resolve these issues before checking out.
I've been searching for ways to edit it without editing the source file. Is there a filter to edit the text on checkout page?
tried this
add_action( 'woocommerce_after_checkout_validation', 'quadlayers', 9999, 2);
function quadlayers( $fields, $errors ){
// in case any validation errors
if( !empty( $errors->get_error_codes() ) ) {
// omit all existing error messages
foreach( $errors->get_error_codes() as $code ) {
$errors->remove( $code );
}
// display custom single error message
$errors->add( 'validation', 'Your Custom Message Goes Here!!!' );
}
}
add_filter('gettext_woocommerce', 'modify_cart_error_message', 10, 3);
function modify_cart_error_message($translation, $text, $domain) {
if ('There are some issues with the items in your cart. Please go back to the cart page and resolve these issues before checking out.' === $translation) {
$translation = 'Your Custom Message Goes Here!!!';
}
return $translation;
}
The cart error message mentioned is coming from /woocommerce/templates/checkout/cart-errors.php file. Either you can override the template in themes or use the above code snippet in the active theme functions.php file
In my checkout form i add google-captcha in function.php. Then i am verifing that captcha is not empty and not false:
function my_custom_checkout_field_process($data, $errors) {
// Check if set, if its not set add an error.
if(count($errors->get_error_messages()) == 0) {
if ( empty( $_REQUEST['g-recaptcha-response'] ) || !(google_recaptcha( $_REQUEST['g-recaptcha-response'] )) ) {
wc_add_notice( __( 'Please enter correct captcha.', 'error' ));
}
}
}
But function wc_add_notice is showing error message only in next page (thankyou.php). Order is sending. But i want thankyou.php is not loading if captcha is false or empty. What should i add to this code? It must function like in the case with required fields. If there is a fault - error message is appeared and user stays on the same page.
I believe you should just remove the if(count($errors->get_error_messages()) == 0) and it should work. You're getting the error on the thank you page because of that if statement.
function my_custom_checkout_field_process($data, $errors) {
if ( empty( $_REQUEST['g-recaptcha-response'] ) || !(google_recaptcha( $_REQUEST['g-recaptcha-response'] )) ) {
throw new Exception( __( 'Please enter correct captcha.', 'error' ));
}
}
When registering user, I am hooking into user_register hook to send some call to the external API.
add_action( 'user_register', 'create_user_on_api' );
inside the create_user_on_api function I am making the call (nonces are there, and tons of security checks, but I'm omitting those for brevity) using wp_remote_post()
function create_user_on_api( $user_id ) {
$user_create_response = wp_remote_post( "someurlgoeshere.api", $curl_args );
if ( is_wp_error( $user_create_response ) ) {
throw new Exception( 'wp error' );
} else {
$code = wp_remote_retrieve_response_code( $user_create_response );
$msg = wp_remote_retrieve_response_message( $user_create_response );
$body = wp_remote_retrieve_body( $user_create_response );
if ( $code !== 200 ) {
throw new Exceprion( 'error' );
}
}
}
This is the gist of it.
The problem is following: using any error handles causes white screen of death, error is logged in my debug.log, and I would like to throw a notice on my admin dashboard that, even though api call failed, the user is created in WP, and you'll have to create it manually on the API or try again.
I tried echoing stuff, custom exception handling mentioned here, but so far no luck.
Any idea how to do that? Can I hook into admin notices in any way?
Ok, found a solution. A hacky one but it works.
On api error I am storing a error message in a transient, which has an expiry of 60 seconds and I created a user_notices function that I hook on admin_notices hook. In it I check two things: if I'm on users screen
$current_screen = get_current_screen();
if ( $current_screen->id === 'users' ) {
}
And then inside this I check if the transients are empty, and then assign them to the $error_message array, which I output in a foreach (in case I have multiple error messages)
if ( ! empty( $error_msg_array ) ) {
foreach ( $error_msg_array as $error_msg_key => $error_msg ) {
?>
<div class="error notice">
<p><?php printf( '<strong>Apigee error:</strong> %s', esc_html( $error_msg ) ); ?></p>
</div>
<?php
}
}
And this seems to be working.
Sometimes, in WooCommerce, the customer is required to fill in street name and house number in a single field.
In this case, we want to then validate the billing_address_1 WooCommerce checkout field to check if it contains numbers before processing the order. We have tried a number of methods to get this done but without any luck.
This standard WooCommerce method doesn't work:
add_action('woocommerce_checkout_process', 'custom_checkout_field_check');
function custom_checkout_field_check() {
// Check if set, if its not set add an error.
if ( $_POST['billing_address_1'] && strpbrk($_POST['billing_address_1'], '1234567890') )
wc_add_notice( __( 'Het adresveld moet minimaal een huisnummer bevatten' ), 'error' );
}
These return bool(false) on the checkout page:
var_dump($_POST['billing_address_1'] == true);
var_dump($_POST['billing_address_2'] == true);
var_dump($_POST['billing_postcode'] == true);
var_dump($_POST['billing_email'] == true);
This front-end workaround doesn't work.
document.querySelector("#place_order").addEventListener("click", validateAddressField);
function validateAddressField () {
console.log('Okay dan!');
}
What else can I try to ensure validation takes place before the order is processed?
// Check if address field contains house number otherwise provide error message
add_action( 'woocommerce_after_checkout_validation', 'validate_checkout', 10, 2);
function validate_checkout( $data, $errors ){
if ( ! preg_match('/[0-9]/', $data[ 'billing_address_1' ] ) ){
$errors->add( 'address', 'Sorry, but the address you provided does not contain a house number.' );
}
}
This isn't working correctly in your code: strpbrk($_POST['billing_address_1'], '1234567890').
he PHP function preg_match() is more appropriate here.
So I have make some little changes in your code to make it work as you expect:
add_action('woocommerce_checkout_process', 'address_field_validation', 10, 0);
function address_field_validation() {
// The field value to check
$post_value = $_POST['billing_address_1'];
// If there is no number in this field value, stops the checkout process
// and displays an error message.
if ( $post_value && ! preg_match( '/[0-9]+/', $post_value ) ) {
// The error message
throw new Exception( sprintf( __( 'Het adresveld moet minimaal een huisnummer bevatten', 'woocommerce' ) ) );
}
}
This code is tested and works on WooCommerce versions 2.6.x and 3.0+…
*This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Reference: WC_Checkout - process_checkout() source code
You can try using hook :- woocommerce_after_checkout_validation
Please refer how to use this hook and sample code here
Let me know if you need anything else...