You are here

uc_free_order.module in UC Free Order Payment Method 5

Same filename and directory in other branches
  1. 6 uc_free_order.module
  2. 7 uc_free_order.module

File

uc_free_order.module
View source
<?php

/**
 * Adds a payment method to handle free orders.
 *
 * The groundwork for this module in core and an initial implementation were
 * made possible by Warner Bros. Records.
 */

/**
 * Implementation of hook_perm().
 */
function uc_free_order_perm() {
  return array(
    'test free order',
  );
}

/**
 * Implementation of hook_form_alter().
 */
function uc_free_order_form_alter($form_id, &$form) {
  global $user;

  // Alter the checkout form to prepare it for our special JS.
  if ($form_id == 'uc_cart_checkout_form' && isset($form['panes']['payment'])) {
    if (user_access('test free order')) {
      drupal_set_message('Test free order: <span style="cursor: pointer;" onclick="alert(\'Discount added.\'); set_line_item(\'fo_discount\', \'Test Discount\', -2000, 0);">Add discount</span> | <span style="cursor: pointer;" onclick="alert(\'Discount removed.\'); remove_line_item(\'fo_discount\');">Remove discount</span>');
    }
    drupal_add_js(drupal_get_path('module', 'uc_free_order') . '/uc_free_order.js');
    $set = FALSE;
    foreach (array_keys($form['panes']['payment']['payment_method']['#options']) as $key) {
      if ($key !== 'free_order' && !$set) {
        drupal_add_js("uc_free_order_next_method = '" . $key . "';", 'inline');
        $set = TRUE;
      }
    }
  }
  elseif ($form_id == 'uc_cart_checkout_review_form') {
    $order = uc_order_load($_SESSION['cart_order']);
    if ($order->payment_method == 'free_order') {
      if ($order->order_total >= 0.01) {
        drupal_set_message(t('We cannot process your order without payment.'), 'error');
        drupal_goto('cart/checkout');
      }
    }
  }
}

/**
 * Implementation of hook_payment_method().
 */
function uc_free_order_payment_method() {
  $methods[] = array(
    'id' => 'free_order',
    'name' => t('Free order'),
    'title' => t('Free order - payment not necessary.'),
    'desc' => t('Allow customers with $0 order totals to checkout without paying.'),
    'callback' => 'uc_payment_method_free_order',
    'checkout' => TRUE,
    'no_gateway' => TRUE,
    'weight' => 10,
  );
  return $methods;
}

// Handles the free order payment method.
function uc_payment_method_free_order($op, &$arg1) {
  switch ($op) {
    case 'cart-details':
      return t('Continue with checkout to complete your free order.');
  }
}