You are here

function commerce_avatax_ui_add_pro_tax_code_fields in Drupal Commerce Connector for AvaTax 7.4

Adds AvaTax code term reference field to the selected content types.

1 string reference to 'commerce_avatax_ui_add_pro_tax_code_fields'
commerce_avatax_ui_admin_settings in includes/commerce_avatax_ui.admin.inc
Admin settings menu callback.

File

includes/commerce_avatax_ui.admin.inc, line 614
Admin settings for commerce_avatax.

Code

function commerce_avatax_ui_add_pro_tax_code_fields($form, &$form_state) {

  // Get out of here if a button other than 'Submit' was clicked.
  if ($form_state['clicked_button']['#value'] != $form['actions']['submit']['#value']) {
    return;
  }
  $product_version = $form_state['values']['commerce_avatax_product_version'];
  if ($product_version == COMMERCE_AVATAX_PRO_VERSION) {

    // Create the taxonomy vocabulary and default avatax code terms.
    $vocab = taxonomy_vocabulary_machine_name_load('avatax_tax_codes');
    if (!$vocab) {
      $vocab = (object) array(
        'name' => t('AvaTax Tax codes'),
        'machine_name' => 'avatax_tax_codes',
      );
      taxonomy_vocabulary_save($vocab);
      taxonomy_term_save((object) array(
        'name' => 'P0000000',
        'vid' => $vocab->vid,
      ));
      taxonomy_term_save((object) array(
        'name' => 'NT',
        'vid' => $vocab->vid,
      ));
    }

    // Get the old configuration.
    $old_product_types = variable_get('commerce_avatax_product_types', array());
    if (empty($old_product_types)) {
      $old_product_types = array_keys(commerce_product_types());
    }
    $field_name = 'avatax_code';
    $new_product_types = $form_state['values']['commerce_avatax_product_types'];
    if (empty($new_product_types)) {
      $new_product_types = array_keys(commerce_product_types());
    }

    // If the old field name and the new field name are identical,
    // just remove the field from product types that are no longer selected.
    if ($old_product_types != $new_product_types) {

      // Remove the field from the product types that have been unselected.
      foreach ($old_product_types as $old_type) {
        if (!in_array($old_type, $new_product_types)) {
          $instance = field_info_instance('commerce_product', $field_name, $old_type);
          if ($instance) {
            field_delete_instance($instance);
          }
        }
      }
    }

    // Create the field type if it doesn't exist.
    $field = field_info_field($field_name);
    if (!$field) {
      $field = field_create_field(array(
        'cardinality' => 1,
        'field_name' => check_plain($field_name),
        'settings' => array(
          'allowed_values' => array(
            0 => array(
              'vocabulary' => 'avatax_tax_codes',
            ),
          ),
        ),
        'type' => 'taxonomy_term_reference',
      ));
    }

    // Create the field instance for the selected product types.
    foreach ($new_product_types as $type) {
      $instance = field_info_instance('commerce_product', $field_name, $type);
      if (!$instance) {
        field_create_instance(array(
          'bundle' => $type,
          // @todo: set default_value
          'default_value' => NULL,
          'display' => array(
            'default' => array(
              'type' => 'hidden',
            ),
            'teaser' => array(
              'type' => 'hidden',
            ),
          ),
          'entity_type' => 'commerce_product',
          'field_name' => check_plain($field_name),
          'label' => 'AvaTax code',
          'required' => TRUE,
          'widget' => array(
            'module' => 'taxonomy',
            'settings' => array(
              'autocomplete_path' => 'taxonomy/autocomplete',
              'size' => 60,
            ),
            'type' => 'taxonomy_autocomplete',
            'weight' => -4,
          ),
        ));
      }
    }
  }
}