You are here

commerce_eu_vat_rc.module in Commerce European Union VAT 7.2

Same filename and directory in other branches
  1. 7 commerce_eu_vat_rc/commerce_eu_vat_rc.module

File

commerce_eu_vat_rc/commerce_eu_vat_rc.module
View source
<?php

/**
 * Implements hook_commerce_customer_profile_type_info().
 */
function commerce_eu_vat_rc_commerce_customer_profile_type_info() {
  $profile_types = array();
  $profile_types['eu_vat_rc'] = array(
    'type' => 'eu_vat_rc',
    'name' => t('EU VAT information'),
    'description' => t('The profile used to collect VAT information on the checkout and order forms.'),
    'addressfield' => FALSE,
    'help' => '',
    'checkout_pane_weight' => 0,
  );
  return $profile_types;
}

/**
 * Add the VAT Number field to VAT Information profile.
 */
function commerce_eu_vat_rc_configure_customer_types() {
  commerce_customer_profile_types_reset();
  if ($profile_type = commerce_customer_profile_type_load('eu_vat_rc')) {
    if (!field_info_field_types('vat_number')) {
      field_cache_clear();
    }

    // Look for or add a VAT number field to the customer profile type.
    $field_name = 'commerce_vat_number';
    $field = field_info_field($field_name);
    $instance = field_info_instance('commerce_customer_profile', $field_name, $profile_type['type']);
    if (empty($field)) {
      $field = array(
        'field_name' => $field_name,
        'type' => 'vat_number',
        'cardinality' => 1,
        'entity_types' => array(
          'commerce_customer_profile',
        ),
        'translatable' => FALSE,
      );
      $field = field_create_field($field);
    }
    if (empty($instance)) {
      $instance = array(
        'field_name' => $field_name,
        'entity_type' => 'commerce_customer_profile',
        'bundle' => $profile_type['type'],
        'label' => t('VAT Number'),
        'required' => FALSE,
        'widget' => array(
          'type' => 'vat_number_widget',
          'weight' => -10,
          'settings' => array(),
        ),
        'display' => array(),
      );

      // Set the default display formatters for various view modes.
      //       foreach (array('default', 'customer', 'administrator') as $view_mode) {
      //         $instance['display'][$view_mode] = array(
      //           'label' => 'hidden',
      //           'type' => 'vat_number_formatter',
      //           'weight' => -10,
      //         );
      //       }
      field_create_instance($instance);
    }
  }
}
function commerce_eu_vat_rc_form_commerce_checkout_form_checkout_alter(&$form, $form_state, $form_id) {
  if (isset($form['customer_profile_eu_vat_rc'])) {
    $eu_countries = commerce_eu_vat_countries();
    $visible_conditions = array();
    foreach ($eu_countries as $iso => $eu_country) {
      $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,
      ),
    );
  }
}