Set a unique validation error notice in Woocommerc

2019-02-15 19:44发布

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

Checkout page

4条回答
孤傲高冷的网名
2楼-- · 2019-02-15 20:34

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楼-- · 2019-02-15 20:47

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

查看更多
可以哭但决不认输i
4楼-- · 2019-02-15 20:49

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.

enter image description here

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

查看更多
贼婆χ
5楼-- · 2019-02-15 20:49

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

查看更多
登录 后发表回答