You are here

function commerce_stock_form_alter in Commerce Stock 7.2

Same name and namespace in other branches
  1. 7 commerce_stock.module \commerce_stock_form_alter()

Implements hook_form_alter().

Alters the add-to-cart form to show out-of-stock items and add a validator.

File

./commerce_stock.module, line 65
Commerce Stock module.

Code

function commerce_stock_form_alter(&$form, &$form_state, $form_id) {
  $commerce_cart_form = 'commerce_cart_form';
  if (module_exists('commerce_cart_view_override')) {
    $commerce_cart_form = variable_get('commerce_cart_view_override_page_view', 'commerce_cart_form');
  }
  if (strpos($form_id, "commerce_cart_add_to_cart_form") === 0) {

    // Check if product is disabled.
    if (isset($form['submit']['#attributes']['disabled']) && $form['submit']['#attributes']['disabled'] == 'disabled') {
      return;
    }

    // Check to see if product has options (multiple products using
    // the default dropdown).
    if (isset($form['product_id']['#options'])) {

      // Set validation.
      $form['#validate'][] = 'commerce_stock_add_to_cart_validate';
      commerce_stock_cart_state_validate_options($form_id, $form, $form_state);
    }
    elseif (isset($form['product_id']['#value'])) {

      // @todo new rules event for handling options - do we need it?
      // Add validation to the add to cart
      $form['#validate'][] = 'commerce_stock_add_to_cart_validate';

      // Check if the add to cart form should be enabled (in stock).
      commerce_stock_cart_state_validate($form_id, $form, $form_state);
    }
  }
  elseif (strpos($form_id, "views_form_{$commerce_cart_form}_") === 0) {
    $view = reset($form_state['build_info']['args']);

    // Only alter buttons if the cart form View shows line items.
    if (!empty($view->result)) {

      // Add validate function to the cart form.
      if (empty($form['actions']['submit']['#validate'])) {
        $form['actions']['submit']['#validate'] = array_merge($form['#validate'], array(
          'commerce_stock_form_commerce_cart_validate',
        ));
      }
      else {
        $form['actions']['submit']['#validate'][] = 'commerce_stock_form_commerce_cart_validate';
      }
      if (empty($form['actions']['checkout']['#validate'])) {
        $form['actions']['checkout']['#validate'] = array_merge($form['#validate'], array(
          'commerce_stock_form_commerce_cart_validate',
        ));
      }
      else {
        $form['actions']['checkout']['#validate'][] = 'commerce_stock_form_commerce_cart_validate';
      }
    }
  }
  elseif ($form_id == 'commerce_checkout_form_checkout') {

    // Add validate function to the checkout form.
    $form['buttons']['continue']['#validate'][] = 'commerce_stock_checkout_form_validate';
  }
  elseif ($form_id == 'commerce_checkout_form_review') {

    // Add validate function to the review form.
    // @todo: would be good to prompt the user with some contextual info
    // as he was about to pay.
    $form['buttons']['continue']['#validate'][] = 'commerce_stock_checkout_form_validate';
  }
}