You are here

function commerce_product_bundle_form_alter in Commerce Product Bundle 7

Same name and namespace in other branches
  1. 8 commerce_product_bundle.module \commerce_product_bundle_form_alter()
  2. 7.2 commerce_product_bundle.module \commerce_product_bundle_form_alter()

Implements of hook_bundle_form_alter().

Here we modify the add to cart form.

File

./commerce_product_bundle.module, line 126
Allows the bundling of products in Drupal Commerce.

Code

function commerce_product_bundle_form_alter(&$form, &$form_state, $form_id) {
  if (strstr($form_id, 'commerce_cart_add_to_cart_form')) {
    if (isset($form_state['default_product'])) {
      $current_product = $form_state['default_product'];
    }
    elseif (isset($form_state['products'])) {
      $current_product = reset($form_state['products']);
      $form_state['default_product'] = $current_product;
    }
    else {
      return;
    }
    $parent_product_id = $current_product->product_id;
    foreach ($current_product as $field_name => $field) {

      // If the field is empty, we have nothing to do here, so skip it.
      if (empty($field)) {
        continue;
      }
      $field_info = field_info_field($field_name);
      $type = $field_info['type'];
      if ($type == 'commerce_product_reference') {
        $field_instance = field_read_instance('commerce_product', $field_name, $current_product->type);

        // Check if the field is enabled for sub products display:
        if (isset($field_instance['display']['default']['type']) && $field_instance['display']['default']['type'] == 'commerce_bundle_product_add_to_cart_form') {
          $lang_code = field_language('commerce_product', $current_product, $field_name);
          $product_ids = array();
          foreach ($field[$lang_code] as $product) {
            $product_ids[] = $product['product_id'];
          }
          $context = isset($form_state['build_info']['args'][2]) ? $form_state['build_info']['args'][2] : array();
          commerce_product_bundle_add_to_cart_form($form, $form_state, $parent_product_id, $product_ids, $field_instance, $field_instance['settings'], $context);
        }
      }
    }
  }
  elseif (strpos($form_id, 'commerce_line_item_views_form_commerce_cart_form_') === 0) {

    // Change any Delete buttons to say Remove.
    if (!empty($form['edit_delete'])) {
      foreach (element_children($form['edit_delete']) as $line_item_id) {
        $form['edit_delete'][$line_item_id]['#submit'][] = 'commerce_product_bundle_line_item_delete_form_submit';
      }
    }
  }
}