Set a unique validation error notice in Woocommerce checkout page

僤鯓⒐⒋嵵緔 提交于 2019-11-30 20:25:43

问题


Woocommerce checkout page shows individual error if the required fields are empty. Normally, if all fields are empty, all errors for those empty fields will be shown:
- First name is a required field
- Last name is a required field
- Street address is a required field
- Town / City is a required field
and so on…

Is it possible to show only one error if all the required fields are empty? Like “ERROR: All fields are empty. Please fill in all required fields to place order.” How to achieve this?

Checkout page


回答1:


To set a unique validation error notice in Woocommerce checkout page you will use the following: (code is mainly from your last question and useful to the community. Just changed to the official function arguments code)

add_action( 'woocommerce_after_checkout_validation', 'checkout_validation_unique_error', 9999, 2 );
function checkout_validation_unique_error( $data, $errors ){
    // Check for any validation errors
    if( ! empty( $errors->get_error_codes() ) ) {

        // Remove all validation errors
        foreach( $errors->get_error_codes() as $code ) {
            $errors->remove( $code );
        }

        // Add a unique custom one
        $errors->add( 'validation', 'Please fill in all required fields to place order.' );
    }
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

Related: Set a unique validation error notice in Woocommerce My Account Addresses and Account Details




回答2:


You would need to disable the current error checking and write custom javascript for the checkout page that would put all the errors in a single message.

There's no native WooCommerce functionality that would accomplish this.

To remove validation, go to your checkout.php template and remove the required attribute from the html fields.

Checkout this link on how to add HTML and javascript to the checkout page.




回答3:


hHi, you can remove validation from checkout fields and add yours:

Here is the hook:

add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );

for more, you can go to

therichpost




回答4:


Its normally show when you press the checkout button. but if you watch it when page load. then maybe it's come by woocommerce bugs. you should update woocommerce plugin. I hope this error would be gone when you will update the plugin.

But after that, if you are facing the same issue contact me. I will look at it.

thank you



来源:https://stackoverflow.com/questions/52987684/set-a-unique-validation-error-notice-in-woocommerce-checkout-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!