How To Not Require Phone Number On Woocommerce
Do your customers find it hard to fill the form at your checkout page? Well, too bad, you just lost a client. Many of the required fields at the WooCommerce checkout page are US-oriented. Many checkout pages oftentimes require filling in a phone number, but only have a phone number formatted in a specific way, with a predefined number of digits. If your e-shop is not US-based, or if it is only you sell internationally, you might frustrate customers who cannot make full in their phone number and cannot keep to finalize their order.
Getting ready
Earlier doing any code modifications, it's e'er a good idea to accept a total backup of our site and make certain that we have FTP access or access to cPanel or Plesk or other control console with access to a file managing director.
If you wonder what is the best way to add together code snippets to your site, accept a look at our previous commodity nigh safely adding PHP code.
1- Make the field optional in theme settings
Many WooCommerce oriented themes take a specific set of options that permit y'all to customize the fields at the checkout folio. Storefront, created by Automattic (the same team backside WooCommerce and WordPress itself), lets you decide which fields you want to make mandatory and which ones not via the Customizer.
So, if you use Storefront, or a like theme that allows for it, simply go to Advent > Customize and then select the WooCommerce tab. Go to Checkout, and from the dropdown under "Telephone Field" select "Optional".
And, that'south it. Now all your customers will have the selection to fill up in the phone field only if they want to.
2- Completely hide telephone field from both "Checkout" and "My account" pages
Making telephone optional in the theme settings in Customizer has 1 drawback: Telephone field is notwithstanding visible (and required) in the "Addresses" tab in "My business relationship". Then, if your customer needs to modify something in their accost, they will not be able to save unless they actually fill in the phone field:
In order to hide the phone field entirely from both "My account" and "Checkout" pages, we volition add the following snippet:
[codesyntax lang="php"]
// Remove billing telephone (and fix email field course to wide) add_filter( 'woocommerce_billing_fields', 'remove_billing_phone_field', 20, i ); function remove_billing_phone_field($fields) { $fields ['billing_phone']['required'] = fake; // To be sure "Not required" $fields['billing_email']['form'] = array('form-row-wide'); // Make the field wide unset( $fields ['billing_phone'] ); // Remove billing telephone field return $fields; } // Remove shipping phone (optional) add_filter( 'woocommerce_shipping_fields', 'remove_shipping_phone_field', 20, 1 ); office remove_shipping_phone_field($fields) { $fields ['shipping_phone']['required'] = false; // To be sure "Non required" unset( $fields ['shipping_phone'] ); // Remove shipping phone field return $fields; } [/codesyntax]
The ii filters we added volition remove the phone field from both "Billing" and "Shipping" address and newly registered customers will non be asked to fill up in the telephone information in the Checkout page.
3- Brand phone field optional on Checkout page based on shipping country
Hither a somewhat more than elaborate scenario: Allow's say your e-shop has some shipping partners in your land and a few more countries. The shipping partner picks up the product from you and delivers it to your customer. However, they crave a phone number, and so that they can contact the customer almost date and 60 minutes of commitment.
In this instance, it would exist very helpful to make telephone field mandatory for the countries where the aircraft company requires it and optional for other countries.
Here is a snippet that volition permit you to practise just that:
[codesyntax lang="php"]
function defined_countries_for_phone_field(){ render array( 'UK', 'BE', 'GE', 'IT', 'ES' ); } // Remove "(optional)" from not required "Billing phone" field add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, four ); role remove_checkout_optional_fields_label( $field, $cardinal, $args, $value ) { // Get the defined countries codes $countries = defined_countries_for_phone_field(); // Go Customer shipping land $shipping_country = WC()->customer->get_shipping_country(); // But on checkout page and My account > Edit accost for billing telephone field if( 'billing_phone' === $key && ( ( is_wc_endpoint_url( 'edit-address' ) && ! in_array($shipping_country, $countries) ) || is_checkout() ) ) { $optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>'; $field = str_replace( $optional, '', $field ); } return $field; } // Make the billing phone field optional (by default) add_filter( 'woocommerce_billing_fields', 'filter_billing_phone_field', 10, 1 ); function filter_billing_phone_field( $fields ) { // Get the divers countries codes $countries = defined_countries_for_phone_field(); // Get Customer shipping country $shipping_country = WC()->client->get_shipping_country(); // But on checkout page and My account > Edit address if ( ( is_wc_endpoint_url( 'edit-accost' ) && ! in_array($shipping_country, $countries) ) || is_checkout() ) $fields['billing_phone']['required'] = false; render $fields; } // Real time shipping country selection actions add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 10, i ); part custom_checkout_scripts_and_fields( $checkout ) { $required = esc_attr__( 'required', 'woocommerce' ); // Get the defined countries codes $countries = defined_countries_for_phone_field(); // Subconscious field for the phone number validation echo '<input type="hidden" name="billing_phone_check" id="billing_phone_check" value="0">'; $countries_str = "'".implode( "', '", $countries )."'"; // Formatting countries for jQuery ?> <script type="text/javascript"> (function($){ var required = '<abbr grade="required" title="<?php repeat $required; ?>">*</abbr>', countries = [<?php repeat $countries_str; ?>], location = $('#shipping_country choice:selected').val(), phoneCheck = 'input#billing_phone_check', phoneField = '#billing_phone_field'; function actionRequire( actionToDo='yep', selector='' ){ if ( actionToDo == 'aye' ) { $(selector).addClass("validate-required"); $(selector+' label').append(required); } else { $(selector).removeClass("validate-required"); $(selector+' label > .required').remove(); } $(selector).removeClass("woocommerce-validated"); $(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field"); } // Default value Once DOM is loaded (with a 300 ms delay) setTimeout( function(){ actionRequire( 'no', phoneField ); if( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == '0' ){ actionRequire( 'yes',phoneField ); $(phoneCheck).val('1'); } }, 300 ); // Alive value $( 'grade.checkout' ).on( 'alter', '#shipping_country', function(){ var location = $('#shipping_country option:selected').val(); if ( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == 0 ) { actionRequire( 'yes' ,phoneField ); $(phoneCheck).val('one'); } else if ( $(phoneCheck).val() == ane ) { actionRequire( 'no' ,phoneField ); $(phoneCheck).val('0'); } }); })(jQuery); </script> <?php } // Phone number validation, when the field is required add_action('woocommerce_checkout_process', 'billing_phone_field_process'); function billing_phone_field_process() { // Check if set, if its not set add together an mistake. if ( ! $_POST['billing_phone'] && $_POST['billing_phone_check'] == 'ane' ) wc_add_notice( __( 'Please enter a number phone.' ), 'error' ); } [/codesyntax]
With the code snippet to a higher place, if the customer wants the product shipped to one of the following countries: UK ('UK'), Belgium ('BE'), Germany ('GE'), Italy ('Information technology') or Spain ('ES'), the phone field volition be mandatory. For every other shipping country, it will be optional.
Wrapping up
Requiring your customers to fill in a phone number in social club to complete checkout can create bug, especially if yous sell internationally and your customer is from a country where the phone number format is different than your own.
- Making the telephone field optional in the "Checkout" page by tweaking the theme settings in Customizer.
- Removing the telephone field from both "Checkout" and "My account" pages with a code snippet
- Making phone field required in the "Checkout" page only if the customer'south shipping country is in a predefined list of countries.
Please let us know in the comments which method works for you meliorate.
I promise you enjoy reading this web log post. If you lot want my team to do WooCommerce Maintenance for you, click here.
How To Not Require Phone Number On Woocommerce,
Source: https://www.themelocation.com/make-phone-field-optional-at-woocommerce-checkout/#:~:text=So%2C%20if%20you%20use%20Storefront,And%2C%20that's%20it.
Posted by: williamsoncomefultall.blogspot.com

0 Response to "How To Not Require Phone Number On Woocommerce"
Post a Comment