a rough snipped to show the idea. vat fields only gets displayed if the selected country is a valid EU country and if the user has filled out company name. otherwise VAT field stays hidden.
That is not possible with the conditional_fields module, because addressfield has its own field structure.

(function ($) {
    Drupal.behaviors.vat_number = {
        attach:function (context, settings) {

            //get initial page values
            var company = $('.organisation-name').val();
            var country = $('.country').val();

            //console.log("country: " + country + " - company: " + company);

            //valid eu countries, taken from drupal, ISO conform
            var eu_countries = [
                "AT",
                "BE",
                "BG",
                "CY",
                "CZ",
                "DE",
                "DK",
                "EE",
                "ES",
                "FI",
                "FR",
                "GB",
                "GR",
                "HU",
                "IE",
                "IT",
                "LT",
                "LU",
                "LV",
                "MT",
                "NL",
                "PL",
                "PT",
                "RO",
                "SE",
                "SI",
                "SK"
            ];

//            var $valid_eu_country = jQuery.inArray(country, eu_countries);
//            console.log("in array: " + $valid_eu_country);

            //show the vat field if country is inside EU and company field has a value too

            //get value of company field
            $('.organisation-name').keyup(function () {
                var company = $('.organisation-name').val();
                //console.log(company);

                //show form or hide it on changes of company field
                if (((jQuery.inArray(country, eu_countries)) >= 0) && (company != '')) {
                    $('.form-item-customer-profile-billing-field-vat-number-und-0-vat-number').show();
                }
                else {
                    $('.form-item-customer-profile-billing-field-vat-number-und-0-vat-number').hide();
                }
            });

            //get value of country select
            $('.country').change(function () {
                var country = $('.country').val();
                //console.log(country);
            });

            //console.log("it is 1: " + jQuery.inArray($('.country').val(), eu_countries));
            //console.log("it is 2: " + jQuery.inArray(country, eu_countries));

            //show form or hide it.
            if (((jQuery.inArray(country, eu_countries)) >= 0) && (company != '')) {
                $('.form-item-customer-profile-billing-field-vat-number-und-0-vat-number').show();
            }
            else {
                $('.form-item-customer-profile-billing-field-vat-number-und-0-vat-number').hide();
            }


        }
    }
}(jQuery));


Comments

dwkitchen’s picture

Version: » 7.x-1.x-dev
Component: VAT Logic » Reverse Charge
Assigned: Unassigned » marcoka
Darren Shelley’s picture

Title: Rough idea for conditional VAT field » Conditionally display EU VAT fieldset at checkout

+1

I would prefer an implementation based on Drupal 7's states API:
http://api.drupal.org/api/examples/form_example%21form_example_states.in...

Currently I use the following code in a custom module, this is easily extendible to encompass company name:

function MODULENAME_form_commerce_checkout_form_checkout_alter(&$form, $form_state, $form_id) {
  if (isset($form['customer_profile_eu_vat_rc'])){
    $eu_countries = _commerce_eu_vat_eu_countries();
    $visible_conditions = array();
    foreach($eu_countries as $iso => $name){
      $visible_conditions[]['value'] = $iso;
    }
    $form['customer_profile_eu_vat_rc']['#states'] = array(
      'visible' => array(
        ':input[name="customer_profile_billing[commerce_customer_address][und][0][country]"]' => $visible_conditions,
      ),
    );
  }
}

I recommend shifting this functionality into the main module if this flow is desired.

dwkitchen’s picture

Status: Active » Fixed

Great thanks Darren added to the Reverse Charge Module

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

adam1’s picture

#2 is added to the Reverse Charge Module? I can't find that rule. And when implementing #2 in a custom module, nothing happens and my VAT-field is not hidden if a country outside the EU is selected. Could someone tell, how I can accomplish the conditional displaying of the VAT number field?

mp’s picture

would need this too for D7, any solution yet?

wizonesolutions’s picture

This is already D7, and it already works. You have to make sure to turn on commerce_eu_vat_rc.