You are here

function commerce_option_set_reference_form_alter in Commerce Product Option 7

Implementation of hook_form_alter()

Here we modify the add to cart form.

File

option_set_reference/commerce_option_set_reference.module, line 161

Code

function commerce_option_set_reference_form_alter(&$form, &$form_state, $form_id) {
  if (strstr($form_id, 'commerce_cart_add_to_cart_form')) {
    if (isset($form_state['default_product'])) {
      $product_id = $form_state['default_product']->product_id;
    }
    elseif (isset($form_state['default_product_id'])) {
      $product_id = $form_state['default_product_id'];
    }
    elseif (isset($form_state['products'])) {
      $current_product = reset($form_state['products']);
      $product_id = $current_product->product_id;
    }
    else {
      return;
    }
    $current_product = commerce_product_load($product_id);
    $someFieldIsAdded = false;
    $options = array();
    if (isset($form_state['line_item']->line_item_id) && $form_state['line_item']->line_item_id > 0 && commerce_product_attributes_access_to_line_item($form_state['line_item']->line_item_id)) {

      // We need to reduce the number of options. We can only have one option per product
      // and per product field and line item. This limits us only in the fact that we can
      // have one option set per one option set reference field.
      foreach (commerce_option_load_by_line_item($form_state['line_item']->line_item_id) as $key => $option) {
        $options[$option->set_id][$option->product_id][$option->field_name][$option->field_delta] = $option;
      }
    }

    // Iterates of the fields of this product. We search for
    // option set reference fields.
    foreach ($current_product as $field_name => $field) {
      $field_info = field_info_field($field_name);
      $type = $field_info['type'];
      if ($type == 'commerce_option_set_reference') {
        $form[$field_name] = array(
          '#tree' => TRUE,
        );
        $lang_code = field_language('commerce_product', $current_product, $field_name);
        if (isset($field[$lang_code])) {
          foreach ($field[$lang_code] as $delta => $set_id) {
            if (count($options) > 0 && isset($options[$set_id['set_id']][$current_product->product_id][$field_name][$delta])) {
              $option = $options[$set_id['set_id']][$current_product->product_id][$field_name][$delta];
            }
            else {
              $option = entity_create('commerce_option', $set_id);
            }
            $form_state['commerce_option'][$field_name][$delta]['option'] = $option;
            $form[$field_name][$delta] = array(
              '#parents' => array(
                $field_name,
                $delta,
              ),
            );
            field_attach_form('commerce_option', $option, $form[$field_name][$delta], $form_state);
            $someFieldIsAdded = true;
          }
        }
      }
    }

    // TODO: Implement the multi options functionality to the bundle integration
    if (isset($form_state['bundle'])) {
      foreach ($form_state['bundle'] as $id => &$bundle_set) {
        $sub_product = $form_state['bundle'][$id]['default_product'];
        $sub_product_wrapper = entity_metadata_wrapper('commerce_product', $sub_product);
        $form[$id] = array(
          '#tree' => TRUE,
        );

        // Iterates of the fields of this product. We search for
        // option set reference fields.
        foreach ($sub_product as $field_name => $field) {
          $field_info = field_info_field($field_name);
          $type = $field_info['type'];
          $form[$id][$field_name] = array(
            '#tree' => TRUE,
          );
          if ($type == 'commerce_option_set_reference') {
            $lang_code = field_language('commerce_product', $sub_product, $field_name);
            if (isset($field[$lang_code])) {
              foreach ($field[$lang_code] as $delta => $set_id) {
                if (count($options) > 0 && isset($options[$set_id['set_id']][$current_product->product_id][$field_name][$delta])) {
                  $option = $options[$set_id['set_id']][$sub_product->product_id][$field_name][$delta];
                }
                else {
                  $option = entity_create('commerce_option', $set_id);
                }
                $form_state[$id]['commerce_option'][$field_name][$delta]['option'] = $option;
                $form[$id][$field_name][$delta] = array(
                  '#parents' => array(
                    $id,
                    $field_name,
                    $delta,
                  ),
                );
                field_attach_form('commerce_option', $option, $form[$id][$field_name][$delta], $form_state);
                $someFieldIsAdded = true;
              }
            }
          }
        }
      }
    }
    if ($someFieldIsAdded) {
      $form['#submit'][] = 'commerce_option_add_to_cart_submit';
    }
  }
}