'WooCommerce edit error message on checkout page
I see WooCommerce gives an error message (You must accept our Terms & Conditions.), if someone not checked the "terms" check box (in the checkout page).
How I change/edit the error message?
Solution 1:[1]
You can do this with the woocommerce_add_error filter. Add the following to your functions.php file.
// 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' );
Solution 2:[2]
I have tried this today and unfortunately this does not work.
This is the code from: woocommerce > classes > class-wc-checkout.php
// Terms
if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && empty( $this->posted['terms'] ) && woocommerce_get_page_id( 'terms' ) > 0 )
$woocommerce->add_error( __( 'You must accept our Terms & Conditions.', 'woocommerce' ) );
It would be great to see a way to edit that You must accept our Terms & Conditions. The original message shows: You must accept our Terms & Conditions I would like it to say: Please confirm your age
I can always hack the core, but I prefer to see if there is a way that you have posted.
Solution 3:[3]
You would have to find the key set by woocommerce for the particular error and then edit it like this, in the example below I'm editing the default text that shows when no shipping methods are available and the customer tries to checkout:
function prefix_edit_error_message( $fields, $errors ) {
$errors->remove( 'shipping' ); // remove default error message
$errors->add( 'shipping', 'My custom text' ); // add our custom error message
}
add_action('woocommerce_after_checkout_validation', 'prefix_edit_error_message', 10, 2);
You can output the contents of errors
to see which error you'd have to remove and then re-add for the desired effect. In your case you'd need to replace shipping
with terms
.
At the time of writing this answer, the hook is located here: https://github.com/woocommerce/woocommerce/blob/6.4.1/plugins/woocommerce/includes/class-wc-checkout.php#L889
It might most likely be called the same thing in future versions of WooCommerce even though its file location might change
Solution 4:[4]
You can always edit the translation files. In this case, you would have to open the .pot file in a PO editor and create a new en_US translation file with the altered strings:
wp-content/plugins/woocommerce/i18n/languages/woocommerce.pot
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andy |
Solution 2 | cmwwebfx |
Solution 3 | Uriahs Victor |
Solution 4 | Stormcloud |