Make Woocommerce checkout phone field optional based on shipping country

后端 未结 2 598
名媛妹妹
名媛妹妹 2020-12-06 03:53

In Woocommerce checkout, I am trying to make the phone field not required for specific shipping countries. Based on \"Make checkout phone field optional for specific cou

2条回答
  •  鱼传尺愫
    2020-12-06 04:18

    Huge thanks to @LoicTheAztec for the original answer, however the solution now gives erratic results and just alternately switches the phone field between required and optional states (on / off).

    The original answer also does not take into account customers who are using the billing address only and have not entered a separate delivery or shipping address.

    Please see the updated version below for 2019

    // SETTINGS: The countries codes (2 capital letters) in the array
    function defined_countries_for_phone_field(){
        return array( 'GB', 'JE', 'GG', 'IM' );
    }
    
    // Remove "(optional)" from non required "Billing phone" field
    add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );
    function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
    
        // Get the defined countries codes
        $countries = defined_countries_for_phone_field();
    
        // Get Customer shipping country
        $shipping_country = WC()->customer->get_shipping_country();
    
        // Only on checkout page and My account > Edit address for billing phone field
        if( 'billing_phone' === $key && ( ( is_wc_endpoint_url( 'edit-address' )
        && in_array($shipping_country, $countries) ) || is_checkout() ) ) {
            $optional = ' (' . esc_html__( 'optional', 'woocommerce' ) . ')';
            $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 defined countries codes
        $countries = defined_countries_for_phone_field();
    
        // Get Customer shipping country
        $shipping_country = WC()->customer->get_shipping_country();
    
        // Only on checkout page and My account > Edit address
        if ( ( is_wc_endpoint_url( 'edit-address' )
        && in_array($shipping_country, $countries) ) || is_checkout() )
            $fields['billing_phone']['required'] = false;
    
        return $fields;
    }
    
    // Real time shipping country selection actions
    add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 10, 1 );
    function custom_checkout_scripts_and_fields( $checkout ) {
        $required = esc_attr__( 'required', 'woocommerce' );
    
        // Get the defined countries codes
        $countries = defined_countries_for_phone_field();
    
        // Hidden field for the phone number validation
        echo '';
        $countries_str = "'".implode( "', '", $countries )."'"; // Formatting countries for jQuery
        ?>
        
        

提交回复
热议问题