WooCommerce allows you to quickly start an e-commerce store.
At some point you may need to customize some of the checkout fields to fit your project requirements.
WooCommerce like other well written plugins uses hooks extensively. Those are actions and filters which allow you or your developer to customize the WooCommerce functionality without hacking the code. Another benefit of using hooks is that those customizations will survive any WooCommerce updates.
Here's a link to the file (next do Save as) : orbisius_quick_fix_wc_ext_opt_co_fields.php
<?php | |
/* | |
This code allows you to make some WooCommerce Checkout Fields optional. | |
Blog post http://orbisius.com/link/3710 | |
Author: Svetoslav Marinov (Slavi) | |
Author URI: http://orbisius.com | |
*/ | |
/* | |
Installation | |
1. Paste the code below in your functions.php OR create an mu-plugin in wp-content/mu-plugins/orbisius_quick_fix_wc_ext_opt_co_fields.php | |
2. Define which fields you want optional by putting this in your wp-config.php | |
You can see the fields on the blog post about this fix at http://orbisius.com/link/3710 | |
define('ORBISIUS_QUICK_FIX_WOO_OPTIONAL_CHECKOUT_FIELDS', 'first_name,last_name' ); | |
3. If you want a customizaiton ($) contact me. | |
*/ | |
add_filter( 'woocommerce_checkout_fields', 'orbisius_quick_fix_wc_ext_opt_co_fields_make_optional_fields', 25); | |
add_filter( 'woocommerce_billing_fields', 'orbisius_quick_fix_wc_ext_opt_co_fields_make_optional_fields', 25); | |
add_filter( 'woocommerce_default_address_fields', 'orbisius_quick_fix_wc_ext_opt_co_fields_make_optional_fields', 25); | |
/** | |
* This function does the heavy lifting to make fields optional. | |
* @param array $fields | |
* @return array | |
* @copyright (c) 2017 Slavi Marinov, http://orbisius.com | |
*/ | |
function orbisius_quick_fix_wc_ext_opt_co_fields_make_optional_fields($fields) { | |
static $optional_fields = null; | |
// Don't know what to process so let's get out of here. | |
if (!defined('ORBISIUS_QUICK_FIX_WOO_OPTIONAL_CHECKOUT_FIELDS')) { | |
return $fields; | |
} | |
if (is_null($optional_fields)) { | |
$optional_fields = preg_split('#\s*[\,\;\|\r\n]+\s*#si', ORBISIUS_QUICK_FIX_WOO_OPTIONAL_CHECKOUT_FIELDS ); | |
$optional_fields = array_unique($optional_fields); | |
$optional_fields = array_filter($optional_fields); | |
} | |
// Nothing to process so let's get out of here. | |
if (empty($optional_fields)) { | |
return $fields; | |
} | |
foreach ($optional_fields as $field_key) { | |
if (isset($fields[$field_key])) { | |
$fields[$field_key]['required'] = false; | |
} | |
if (isset($fields['shipping'][$field_key])) { | |
$fields[$field_key]['shipping']['required'] = false; | |
} | |
if (isset($fields['billing'][$field_key])) { | |
$fields[$field_key]['billing']['required'] = false; | |
} | |
// do we have fields that don't have a prefix? | |
$no_prefix_key = $field_key; | |
$no_prefix_key = preg_replace( '#(billing|shipping)_#si', '', $no_prefix_key); | |
if (isset($fields[$no_prefix_key])) { | |
$fields[$no_prefix_key]['required'] = false; | |
} | |
if (isset($fields['shipping'][$no_prefix_key])) { | |
$fields[$no_prefix_key]['shipping']['required'] = false; | |
} | |
if (isset($fields['billing'][$no_prefix_key])) { | |
$fields[$no_prefix_key]['billing']['required'] = false; | |
} | |
} | |
return $fields; | |
} | |
Here are all of the Shipping & Billing fields from all the filters.
[code]
array (
'first_name' =>
array (
'label' => 'First name',
'required' => false,
'class' =>
array (
0 => 'form-row-first',
),
'autocomplete' => 'given-name',
'autofocus' => true,
'priority' => 10,
),
'last_name' =>
array (
'label' => 'Last name',
'required' => false,
'class' =>
array (
0 => 'form-row-last',
),
'autocomplete' => 'family-name',
'priority' => 20,
),
'company' =>
array (
'label' => 'Company name',
'class' =>
array (
0 => 'form-row-wide',
),
'autocomplete' => 'organization',
'priority' => 30,
),
'country' =>
array (
'type' => 'country',
'label' => 'Country',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
2 => 'update_totals_on_change',
),
'autocomplete' => 'country',
'priority' => 40,
),
'address_1' =>
array (
'label' => 'Street address',
'placeholder' => 'House number and street name',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-line1',
'priority' => 50,
),
'address_2' =>
array (
'placeholder' => 'Apartment, suite, unit etc. (optional)',
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'required' => false,
'autocomplete' => 'address-line2',
'priority' => 60,
),
'city' =>
array (
'label' => 'Town / City',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-level2',
'priority' => 70,
),
'state' =>
array (
'type' => 'state',
'label' => 'State / County',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'state',
),
'autocomplete' => 'address-level1',
'priority' => 80,
),
'postcode' =>
array (
'label' => 'Postcode / ZIP',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'postcode',
),
'autocomplete' => 'postal-code',
'priority' => 90,
),
)
array (
'first_name' =>
array (
'label' => 'First name',
'required' => false,
'class' =>
array (
0 => 'form-row-first',
),
'autocomplete' => 'given-name',
'autofocus' => true,
'priority' => 10,
),
'last_name' =>
array (
'label' => 'Last name',
'required' => false,
'class' =>
array (
0 => 'form-row-last',
),
'autocomplete' => 'family-name',
'priority' => 20,
),
'company' =>
array (
'label' => 'Company name',
'class' =>
array (
0 => 'form-row-wide',
),
'autocomplete' => 'organization',
'priority' => 30,
),
'country' =>
array (
'type' => 'country',
'label' => 'Country',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
2 => 'update_totals_on_change',
),
'autocomplete' => 'country',
'priority' => 40,
),
'address_1' =>
array (
'label' => 'Street address',
'placeholder' => 'House number and street name',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-line1',
'priority' => 50,
),
'address_2' =>
array (
'placeholder' => 'Apartment, suite, unit etc. (optional)',
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'required' => false,
'autocomplete' => 'address-line2',
'priority' => 60,
),
'city' =>
array (
'label' => 'Town / City',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-level2',
'priority' => 70,
),
'state' =>
array (
'type' => 'state',
'label' => 'State / County',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'state',
),
'autocomplete' => 'address-level1',
'priority' => 80,
),
'postcode' =>
array (
'label' => 'Postcode / ZIP',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'postcode',
),
'autocomplete' => 'postal-code',
'priority' => 90,
),
)
array (
'billing_first_name' =>
array (
'label' => 'First name',
'required' => false,
'class' =>
array (
0 => 'form-row-first',
),
'autocomplete' => 'given-name',
'autofocus' => true,
'priority' => 10,
),
'billing_last_name' =>
array (
'label' => 'Last name',
'required' => false,
'class' =>
array (
0 => 'form-row-last',
),
'autocomplete' => 'family-name',
'priority' => 20,
),
'billing_company' =>
array (
'label' => 'Company name',
'class' =>
array (
0 => 'form-row-wide',
),
'autocomplete' => 'organization',
'priority' => 30,
),
'billing_country' =>
array (
'type' => 'country',
'label' => 'Country',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
2 => 'update_totals_on_change',
),
'autocomplete' => 'country',
'priority' => 40,
),
'billing_address_1' =>
array (
'label' => 'Street address',
'placeholder' => 'House number and street name',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-line1',
'priority' => 50,
),
'billing_address_2' =>
array (
'placeholder' => 'Apartment, suite, unit etc. (optional)',
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'required' => false,
'autocomplete' => 'address-line2',
'priority' => 60,
),
'billing_city' =>
array (
'label' => 'Town / City',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-level2',
'priority' => 70,
),
'billing_state' =>
array (
'type' => 'state',
'label' => 'Province',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'state',
),
'autocomplete' => 'address-level1',
'priority' => 80,
'country_field' => 'billing_country',
),
'billing_postcode' =>
array (
'label' => 'Postcode / ZIP',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'postcode',
),
'autocomplete' => 'postal-code',
'priority' => 90,
),
'billing_phone' =>
array (
'label' => 'Phone',
'required' => true,
'type' => 'tel',
'class' =>
array (
0 => 'form-row-first',
),
'validate' =>
array (
0 => 'phone',
),
'autocomplete' => 'tel',
'priority' => 100,
),
'billing_email' =>
array (
'label' => 'Email address',
'required' => true,
'type' => 'email',
'class' =>
array (
0 => 'form-row-last',
),
'validate' =>
array (
0 => 'email',
),
'autocomplete' => 'email username',
'priority' => 110,
),
)
array (
'first_name' =>
array (
'label' => 'First name',
'required' => false,
'class' =>
array (
0 => 'form-row-first',
),
'autocomplete' => 'given-name',
'autofocus' => true,
'priority' => 10,
),
'last_name' =>
array (
'label' => 'Last name',
'required' => false,
'class' =>
array (
0 => 'form-row-last',
),
'autocomplete' => 'family-name',
'priority' => 20,
),
'company' =>
array (
'label' => 'Company name',
'class' =>
array (
0 => 'form-row-wide',
),
'autocomplete' => 'organization',
'priority' => 30,
),
'country' =>
array (
'type' => 'country',
'label' => 'Country',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
2 => 'update_totals_on_change',
),
'autocomplete' => 'country',
'priority' => 40,
),
'address_1' =>
array (
'label' => 'Street address',
'placeholder' => 'House number and street name',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-line1',
'priority' => 50,
),
'address_2' =>
array (
'placeholder' => 'Apartment, suite, unit etc. (optional)',
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'required' => false,
'autocomplete' => 'address-line2',
'priority' => 60,
),
'city' =>
array (
'label' => 'Town / City',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-level2',
'priority' => 70,
),
'state' =>
array (
'type' => 'state',
'label' => 'State / County',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'state',
),
'autocomplete' => 'address-level1',
'priority' => 80,
),
'postcode' =>
array (
'label' => 'Postcode / ZIP',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'postcode',
),
'autocomplete' => 'postal-code',
'priority' => 90,
),
)
array (
'billing' =>
array (
'billing_first_name' =>
array (
'label' => 'First name',
'required' => false,
'class' =>
array (
0 => 'form-row-first',
),
'autocomplete' => 'given-name',
'autofocus' => true,
'priority' => 10,
),
'billing_last_name' =>
array (
'label' => 'Last name',
'required' => false,
'class' =>
array (
0 => 'form-row-last',
),
'autocomplete' => 'family-name',
'priority' => 20,
),
'billing_company' =>
array (
'label' => 'Company name',
'class' =>
array (
0 => 'form-row-wide',
),
'autocomplete' => 'organization',
'priority' => 30,
),
'billing_country' =>
array (
'type' => 'country',
'label' => 'Country',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
2 => 'update_totals_on_change',
),
'autocomplete' => 'country',
'priority' => 40,
),
'billing_address_1' =>
array (
'label' => 'Street address',
'placeholder' => 'House number and street name',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-line1',
'priority' => 50,
),
'billing_address_2' =>
array (
'placeholder' => 'Apartment, suite, unit etc. (optional)',
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'required' => false,
'autocomplete' => 'address-line2',
'priority' => 60,
),
'billing_city' =>
array (
'label' => 'Town / City',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-level2',
'priority' => 70,
),
'billing_state' =>
array (
'type' => 'state',
'label' => 'Province',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'state',
),
'autocomplete' => 'address-level1',
'priority' => 80,
'country_field' => 'billing_country',
),
'billing_postcode' =>
array (
'label' => 'Postcode / ZIP',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'postcode',
),
'autocomplete' => 'postal-code',
'priority' => 90,
),
'billing_phone' =>
array (
'label' => 'Phone',
'required' => true,
'type' => 'tel',
'class' =>
array (
0 => 'form-row-first',
),
'validate' =>
array (
0 => 'phone',
),
'autocomplete' => 'tel',
'priority' => 100,
),
'billing_email' =>
array (
'label' => 'Email address',
'required' => true,
'type' => 'email',
'class' =>
array (
0 => 'form-row-last',
),
'validate' =>
array (
0 => 'email',
),
'autocomplete' => 'email username',
'priority' => 110,
),
),
'shipping' =>
array (
'shipping_first_name' =>
array (
'label' => 'First name',
'required' => false,
'class' =>
array (
0 => 'form-row-first',
),
'autocomplete' => 'given-name',
'autofocus' => true,
'priority' => 10,
),
'shipping_last_name' =>
array (
'label' => 'Last name',
'required' => false,
'class' =>
array (
0 => 'form-row-last',
),
'autocomplete' => 'family-name',
'priority' => 20,
),
'shipping_company' =>
array (
'label' => 'Company name',
'class' =>
array (
0 => 'form-row-wide',
),
'autocomplete' => 'organization',
'priority' => 30,
),
'shipping_country' =>
array (
'type' => 'country',
'label' => 'Country',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
2 => 'update_totals_on_change',
),
'autocomplete' => 'country',
'priority' => 40,
),
'shipping_address_1' =>
array (
'label' => 'Street address',
'placeholder' => 'House number and street name',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-line1',
'priority' => 50,
),
'shipping_address_2' =>
array (
'placeholder' => 'Apartment, suite, unit etc. (optional)',
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'required' => false,
'autocomplete' => 'address-line2',
'priority' => 60,
),
'shipping_city' =>
array (
'label' => 'Town / City',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'autocomplete' => 'address-level2',
'priority' => 70,
),
'shipping_state' =>
array (
'type' => 'state',
'label' => 'Province',
'required' => false,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'state',
),
'autocomplete' => 'address-level1',
'priority' => 80,
'country_field' => 'shipping_country',
),
'shipping_postcode' =>
array (
'label' => 'Postcode / ZIP',
'required' => true,
'class' =>
array (
0 => 'form-row-wide',
1 => 'address-field',
),
'validate' =>
array (
0 => 'postcode',
),
'autocomplete' => 'postal-code',
'priority' => 90,
),
),
'account' =>
array (
'account_password' =>
array (
'type' => 'password',
'label' => 'Account password',
'required' => true,
'placeholder' => 'Password',
),
),
'order' =>
array (
'order_comments' =>
array (
'type' => 'textarea',
'class' =>
array (
0 => 'notes',
),
'label' => 'Order notes',
'placeholder' => 'Notes about your order, e.g. special notes for delivery.',
),
),
'billing_first_name' =>
array (
'billing' =>
array (
'required' => false,
),
),
'billing_last_name' =>
array (
'billing' =>
array (
'required' => false,
),
),
'billing_city' =>
array (
'billing' =>
array (
'required' => false,
),
),
'billing_state' =>
array (
'billing' =>
array (
'required' => false,
),
),
'shipping_first_name' =>
array (
'shipping' =>
array (
'required' => false,
),
),
'shipping_last_name' =>
array (
'shipping' =>
array (
'required' => false,
),
),
'shipping_city' =>
array (
'shipping' =>
array (
'required' => false,
),
),
'shipping_state' =>
array (
'shipping' =>
array (
'required' => false,
),
),
)
[/code]
Related
- https://www.skyverge.com/blog/how-to-edit-required-woocommerce-checkout-fields/
- https://stackoverflow.com/questions/37791580/woocommerce-disabling-required-from-checkout-fields-with-a-filter-hook
- https://gist.github.com/corsonr/6129163
- https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
- https://wordpress.org/support/topic/want-to-disable-required-field-on-checkout-page/