You are here

function commerce_cart_form_alter in Commerce Core 7

Implements hook_form_alter().

File

modules/cart/commerce_cart.module, line 244
Implements the shopping cart system and add to cart features.

Code

function commerce_cart_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'views_form_commerce_cart_form_') === 0) {

    // Only alter buttons if the cart form View shows line items.
    $view = reset($form_state['build_info']['args']);
    if (!empty($view->result)) {

      // Change the Save button to say Update cart.
      $form['actions']['submit']['#value'] = t('Update cart');
      $form['actions']['submit']['#submit'] = array_merge($form['#submit'], array(
        'commerce_cart_line_item_views_form_submit',
      ));

      // Change any Delete buttons to say Remove.
      if (!empty($form['edit_delete'])) {
        foreach (element_children($form['edit_delete']) as $key) {

          // Load and wrap the line item to have the title in the submit phase.
          if (!empty($form['edit_delete'][$key]['#line_item_id'])) {
            $line_item_id = $form['edit_delete'][$key]['#line_item_id'];
            $form_state['line_items'][$line_item_id] = commerce_line_item_load($line_item_id);
            $form['edit_delete'][$key]['#value'] = t('Remove');
            $form['edit_delete'][$key]['#submit'] = array_merge($form['#submit'], array(
              'commerce_cart_line_item_delete_form_submit',
            ));
          }
        }
      }
    }
    else {

      // Otherwise go ahead and remove any buttons from the View.
      unset($form['actions']);
    }
  }
  elseif (strpos($form_id, 'commerce_checkout_form_') === 0 && !empty($form['buttons']['cancel'])) {

    // Override the submit handler for changing the order status on checkout cancel.
    foreach ($form['buttons']['cancel']['#submit'] as $key => &$value) {
      if ($value == 'commerce_checkout_form_cancel_submit') {
        $value = 'commerce_cart_checkout_form_cancel_submit';
      }
    }
  }
  elseif (strpos($form_id, 'views_form_commerce_cart_block') === 0) {

    // No point in having a "Save" button on the shopping cart block.
    unset($form['actions']);
  }
}