You are here

function commerce_option_add_to_cart_submit in Commerce Product Option 7

Same name and namespace in other branches
  1. 7.2 commerce_option.module \commerce_option_add_to_cart_submit()

Cart submit callback function. This is required to create / update the option related to the line item.

Parameters

$form Form array:

$form_state The form state array.:

Return value

void

1 string reference to 'commerce_option_add_to_cart_submit'
commerce_option_set_reference_form_alter in option_set_reference/commerce_option_set_reference.module
Implementation of hook_form_alter()

File

option_set_reference/commerce_option_set_reference.module, line 298

Code

function commerce_option_add_to_cart_submit($form, $form_state) {
  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);
  foreach ($current_product as $field_name => $field) {
    $field_info = field_info_field($field_name);
    $type = $field_info['type'];
    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);

        // 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'];
          if ($type == 'commerce_option_set_reference') {
            $lang_code = field_language('commerce_product', $sub_product, $field_name);
            foreach ($field[$lang_code] as $delta => $set_id) {
              $option = $form_state[$id]['commerce_option'][$field_name][$delta];

              // Notify field widgets.
              field_attach_submit('commerce_option', $option, $form[$id][$field_name][$delta], $form_state);

              //$option->line_item_id = $form_state['bundle_product_line_items'][$sub_product_wrapper->product_id->value()]->line_item_id;
              $option->line_item_id = $form_state['line_item']->line_item_id;
              $option->field_name = $field_name;
              $option->field_delta = $delta;
              $option->product_id = $sub_product->product_id;

              // Save the product.
              commerce_option_save($option);
            }
          }
        }
      }
    }
    else {
      if ($type == 'commerce_option_set_reference') {
        $lang_code = field_language('commerce_product', $current_product, $field_name);
        foreach ($field[$lang_code] as $delta => $set_id) {
          $option = $form_state['commerce_option'][$field_name][$delta]['option'];

          // Notify field widgets. // entity_form_submit_build_entity
          field_attach_submit('commerce_option', $option, $form[$field_name][$delta], $form_state);
          $option->line_item_id = $form_state['line_item']->line_item_id;
          $option->field_name = $field_name;
          $option->field_delta = $delta;
          $option->product_id = $current_product->product_id;

          // Save the product.
          commerce_option_save($option);
        }
      }
    }
  }
}