You are here

function uc_recurring_product_form_uc_cart_checkout_form_alter in UC Recurring Payments and Subscriptions 7.2

Same name and namespace in other branches
  1. 6.2 modules/uc_recurring_product/uc_recurring_product.module \uc_recurring_product_form_uc_cart_checkout_form_alter()

Implements hook_form_FORM-ID_alter().

See also

uc_cart_checkout_form()

File

modules/uc_recurring_product/uc_recurring_product.module, line 375
Add recurring payments/fees to a product. This is imlpemented through Ubercarts product features.

Code

function uc_recurring_product_form_uc_cart_checkout_form_alter(&$form, $form_state) {

  // We may need to alter the checkout form to remove invalid payment methods.
  if (isset($form['panes']['payment'])) {
    $order = new stdClass();
    $order->products = uc_cart_get_contents();

    // Make no changes if no recurring fees are found.
    if (uc_recurring_product_get_recurring_products_in_order($order) == array()) {
      return;
    }

    // If configured, display a message about the recurring fees.
    if ($message = variable_get('uc_recurring_checkout_message', '')) {
      drupal_set_message(check_markup($message));
    }

    // Remove invalid payment methods from the payment pane.
    $valid = variable_get('uc_recurring_payment_methods', array());
    if (!empty($form['panes']['payment']['payment_method']['#options'])) {
      foreach (array_keys($form['panes']['payment']['payment_method']['#options']) as $key) {
        if (isset($valid[$key]) && $valid[$key] === 0 || !uc_recurring_payment_method_supported($key)) {
          unset($form['panes']['payment']['payment_method']['#options'][$key]);
        }
      }
      $count = count($form['panes']['payment']['payment_method']['#options']);
      if ($count == 0) {

        // Display an error message if no payment methods remain.
        if (user_access('administer recurring fees')) {
          drupal_set_message(t('There are no payment methods configured for orders with recurring fees, enable one from <a href="@url">recurring fee admin settings</a>.', array(
            '@url' => url('admin/store/settings/products/edit/features'),
          )), 'error');
        }
      }
      elseif ($count == 1) {

        // If only one payment method remains, make it the default.
        $payment_method_keys = array_keys($form['panes']['payment']['payment_method']['#options']);
        $form['panes']['payment']['payment_method']['#default_value'] = array_pop($payment_method_keys);
      }
    }
  }
}