Add controls to control if address 2, company, and phone fields are optional, required, or hidden.

This commit is contained in:
Mike Jolley 2018-04-05 17:51:43 +01:00
parent e30b8e8a5f
commit 46815d1d01
2 changed files with 74 additions and 10 deletions

View File

@ -633,6 +633,7 @@ class WC_Countries {
'class' => array( 'form-row-wide' ),
'autocomplete' => 'organization',
'priority' => 30,
'required' => 'required' === get_option( 'woocommerce_checkout_company_field', 'optional' ),
),
'country' => array(
'type' => 'country',
@ -652,11 +653,12 @@ class WC_Countries {
'priority' => 50,
),
'address_2' => array(
'placeholder' => esc_attr__( 'Apartment, suite, unit etc. (optional)', 'woocommerce' ),
'placeholder' => esc_attr__( 'Apartment, suite, unit etc.', 'woocommerce' ),
'class' => array( 'form-row-wide', 'address-field' ),
'required' => false,
'autocomplete' => 'address-line2',
'priority' => 60,
'required' => 'required' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ),
),
'city' => array(
'label' => __( 'Town / City', 'woocommerce' ),
@ -684,6 +686,22 @@ class WC_Countries {
),
);
if ( 'hidden' === get_option( 'woocommerce_checkout_company_field', 'optional' ) ) {
unset( $fields['company'] );
}
$address_2_visibility = get_option( 'woocommerce_checkout_address_2_field', 'optional' );
if ( 'optional' === $address_2_visibility && false === wc_string_to_bool( get_option( 'woocommerce_checkout_label_required_fields', 'yes' ) ) ) {
$fields['address_2']['placeholder'] .= ' (' . esc_html__( 'optional', 'woocommerce' ) . ')';
} elseif ( 'hidden' === $address_2_visibility ) {
unset( $fields['address_2'] );
}
if ( 'hidden' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ) ) {
unset( $fields['address_2'] );
}
return apply_filters( 'woocommerce_default_address_fields', $fields );
}
@ -1200,15 +1218,17 @@ class WC_Countries {
// Add email and phone fields.
if ( 'billing_' === $type ) {
$address_fields['billing_phone'] = array(
'label' => __( 'Phone', 'woocommerce' ),
'required' => true,
'type' => 'tel',
'class' => array( 'form-row-wide' ),
'validate' => array( 'phone' ),
'autocomplete' => 'tel',
'priority' => 100,
);
if ( 'hidden' !== get_option( 'woocommerce_checkout_phone_field', 'optional' ) ) {
$address_fields['billing_phone'] = array(
'label' => __( 'Phone', 'woocommerce' ),
'required' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'optional' ),
'type' => 'tel',
'class' => array( 'form-row-wide' ),
'validate' => array( 'phone' ),
'autocomplete' => 'tel',
'priority' => 100,
);
}
$address_fields['billing_email'] = array(
'label' => __( 'Email address', 'woocommerce' ),
'required' => true,

View File

@ -697,6 +697,50 @@ class WC_Shop_Customizer {
),
)
);
// Checkout field controls.
$fields = array(
'company' => __( 'Company name', 'woocommerce' ),
'address_2' => __( 'Address line 2', 'woocommerce' ),
'phone' => __( 'Phone', 'woocommerce' ),
);
foreach ( $fields as $field => $label ) {
$wp_customize->add_setting(
'woocommerce_checkout_' . $field . '_field',
array(
'default' => 'optional',
'type' => 'option',
'capability' => 'manage_woocommerce',
'sanitize_callback' => array( $this, 'sanitize_checkout_field_display' ),
)
);
$wp_customize->add_control(
'woocommerce_checkout_' . $field . '_field',
array(
/* Translators: %s field name. */
'label' => sprintf( __( '%s field', 'woocommerce' ), $label ),
'section' => 'woocommerce_checkout',
'settings' => 'woocommerce_checkout_' . $field . '_field',
'type' => 'select',
'choices' => array(
'hidden' => __( 'Hidden', 'woocommerce' ),
'optional' => __( 'Optional', 'woocommerce' ),
'required' => __( 'Required', 'woocommerce' ),
),
)
);
}
}
/**
* Sanitize field display.
*
* @param string $value '', 'subcategories', or 'both'.
* @return string
*/
public function sanitize_checkout_field_display( $value ) {
$options = array( 'hidden', 'optional', 'required' );
return in_array( $value, $options, true ) ? $value : '';
}
}