Commit ab445f42 authored by Eric Goodwin's avatar Eric Goodwin Committed by Si Nguyen
Browse files

Issue #1873480 by Eric115: Disable checkout button when order is invalid

parent 25230216
Loading
Loading
Loading
Loading
+1 −0
Changes for commerce_moa.install: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -11,4 +11,5 @@ function commerce_moa_uninstall() {
  variable_del('commerce_minimum_order_amount');
  variable_del('commerce_maximum_order_amount');
  variable_del('commerce_minimum_order_quantity');
  variable_del('commerce_moa_disable_button_on_invalid');
}
+111 −0
Changes for commerce_moa.module: 111 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -82,6 +82,13 @@ function commerce_moa_settings_form($form, &$form_state) {
    )),
    '#default_value' => commerce_moa_currency_format(),
  );
  $form['commerce_moa_disable_button_on_invalid'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disabled checkout/continue buttons on invalid orders'),
    '#description' => t('If an order does not meet the above criteria, a warning
    message will be displayed and the checkout/continue button disabled.'),
    '#default_value' => commerce_moa_disable_button_on_invalid(),
  );

  $form['#validate'] = array('commerce_moa_settings_form_validate');

@@ -132,6 +139,13 @@ function commerce_moa_currency_format() {
  return variable_get('commerce_minimum_currency_format', 1);
}

/**
 * Returns disabled checkout/continue button setting.
 */
function commerce_moa_disable_button_on_invalid() {
  return variable_get('commerce_moa_disable_button_on_invalid', 1);
}

/**
 * Implements hook_form_alter().
 */
@@ -143,15 +157,112 @@ function commerce_moa_form_alter(&$form, &$form_state, $form_id) {
  if ($minimum_value > 0 || $maximum_value > 0 || $minimum_quantity > 0) {
    if ($form_id == 'views_form_commerce_cart_form_default') {
      $form['actions']['checkout']['#validate'][] = 'commerce_moa_checkout_form_validate';

      if (commerce_moa_disable_button_on_invalid()) {
        $valid = commerce_moa_is_valid_order($form_state['order']);
        if (!$valid) {
          $form['actions']['checkout']['#attributes']['disabled'] = TRUE;
        }
      }
    }
    elseif ($form_id == 'commerce_checkout_form_checkout') {
      $form['buttons']['continue']['#validate'][] = 'commerce_moa_checkout_form_validate';

      if (commerce_moa_disable_button_on_invalid()) {
        $valid = commerce_moa_is_valid_order($form_state['order']);
        if (!$valid) {
          $form['buttons']['continue']['#attributes']['disabled'] = TRUE;
        }
      }
    }
    elseif ($form_id == 'commerce_checkout_form_review') {
      $form['buttons']['continue']['#validate'][] = 'commerce_moa_checkout_form_validate';

      if (commerce_moa_disable_button_on_invalid()) {
        $valid = commerce_moa_is_valid_order($form_state['order']);
        if (!$valid) {
          $form['buttons']['continue']['#attributes']['disabled'] = TRUE;
        }
      }
    }
  }
}

/**
 * Check if an order is valid in terms of minimum and maximum settings.
 *
 * This will return FALSE on the first invalidity it finds, in the order of
 * quantity, minimum order amount and then maximum order amount.
 *
 * @param \stdClass $order
 *   Commerce order to validate.
 *
 * @param bool $set_warning
 *   If TRUE, a warning message will be set if the order is considered invalid.
 *   This gives users more information about why the checkout/continue button
 *   is disabled (e.g. Their order is less than the minimum).
 *
 * @return bool
 *   FALSE if the order does not meet one of the set criteria (min quantity,
 *   min order amount or max order amount), TRUE otherwise.
 */
function commerce_moa_is_valid_order($order, $set_warning = TRUE) {
  $balance = $order->commerce_order_total[LANGUAGE_NONE][0];
  $total = commerce_currency_amount_to_decimal($balance['amount'], $balance['currency_code']);
  $minimum_quantity = commerce_moa_default_quantity();
  $minimum_value = (float) commerce_moa_default_minimum();
  $maximum_value = (float) commerce_moa_default_maximum();

  $use_custom_currency_format = commerce_moa_currency_format();
  $minimum_value_error = commerce_moa_format_amount($minimum_value, $balance['currency_code'], $use_custom_currency_format);
  $maximum_value_error = commerce_moa_format_amount($maximum_value, $balance['currency_code'], $use_custom_currency_format);

  // Check access quantity.
  if (user_access('access quantity')) {
    if ($minimum_quantity > 0) {
      $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
      $order_wrapper->commerce_line_items;
      foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
        // Don't check quantity for shipping line item.
        $is_product = in_array($line_item_wrapper->type->value(), commerce_product_line_item_types());
        if ($is_product && intval($line_item_wrapper->quantity->value()) < $minimum_quantity) {
          if ($set_warning) {
            $warning = t('This store requires minimum quantity order of
            !minimum_quantity per item.',
            array('!minimum_quantity' => $minimum_quantity));
            drupal_set_message($warning, 'warning', FALSE);
          }

          return FALSE;
        }
      }
    }
  }
  // Check access minimum.
  if (user_access('access minimum')) {
    if ($minimum_value > 0 && $total < $minimum_value) {
      if ($set_warning) {
        $warning = t('This store requires minimum order of !minimum_amount.',
          array('!minimum_amount' => $minimum_value_error));
        drupal_set_message($warning, 'warning', FALSE);
      }
      return FALSE;
    }
  }
  // Check access maximum.
  if (user_access('access maximum')) {
    if ($maximum_value > 0 && $total > $maximum_value) {
      if ($set_warning) {
        $warning = t('This store requires maximum order of !maximum_amount.',
          array('!maximum_amount' => $maximum_value_error));
        drupal_set_message($warning, 'warning', FALSE);
      }
      return FALSE;
    }
  }

  return TRUE;
}

/**
 * The validate when direct access to checkout page.