You are here

function uc_checkout_pane_coupon in Ubercart Discount Coupons 5

Same name and namespace in other branches
  1. 6 uc_coupon.module \uc_checkout_pane_coupon()
  2. 7.3 uc_coupon.module \uc_checkout_pane_coupon()
  3. 7.2 uc_coupon.module \uc_checkout_pane_coupon()

Checkout Pane callback function.

Used to display a form in the checkout process so that customers can enter discount coupons.

1 string reference to 'uc_checkout_pane_coupon'
uc_coupon_checkout_pane in ./uc_coupon.module
Implementation of hook_checkout_pane().

File

./uc_coupon.module, line 1020
Provides discount coupons for Ubercart.

Code

function uc_checkout_pane_coupon($op, &$arg1, $arg2) {
  switch ($op) {
    case 'view':
      uc_add_js(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.js');
      if ($_SESSION['uc_coupon']) {
        $code = $_SESSION['uc_coupon'];
        unset($_SESSION['uc_coupon']);
      }
      else {
        $code = $arg1->data['coupon'];
      }
      $coupon = uc_coupon_validate($code);
      if ($coupon->valid) {
        $settings = array(
          'title' => $coupon->title,
          'amount' => $coupon->amount,
        );
        uc_add_js(array(
          'uc_coupon' => $settings,
        ), 'setting');
      }
      $description = variable_get('uc_coupon_pane_description', 'Enter a coupon code for this order.');
      $contents['code'] = array(
        '#type' => 'textfield',
        '#title' => t('Coupon code'),
        '#default_value' => $code,
        '#size' => 25,
      );
      $contents['apply'] = array(
        '#type' => 'button',
        '#value' => t('Apply to order'),
        '#attributes' => array(
          'onclick' => "getCoupon(); return false;",
        ),
      );
      return array(
        'description' => $description,
        'contents' => $contents,
      );
    case 'process':
      if ($arg2['code']) {
        $code = strtoupper(check_plain($arg2['code']));
        $arg1->data['coupon'] = $code;
        $coupon = uc_coupon_validate($code);
        if (!$coupon->valid) {
          drupal_set_message($coupon->message, 'error');
          return FALSE;
        }
        $result = db_query("SELECT line_item_id FROM {uc_order_line_items} WHERE order_id = %d AND type = 'coupon'", $arg1->order_id);
        if ($lid = db_result($result)) {
          db_query("UPDATE {uc_coupons_orders} SET cid = %d, code = '%s', value = %f WHERE oid = %d", $coupon->cid, $code, $coupon->amount, $arg1->order_id);
          uc_order_update_line_item($lid, $coupon->title, -$coupon->amount);
        }
        else {
          db_query("INSERT INTO {uc_coupons_orders} (cid, oid, code, value) VALUES (%d, %d, '%s', %f)", $coupon->cid, $arg1->order_id, $code, $coupon->amount);
          uc_order_line_item_add($arg1->order_id, 'coupon', $coupon->title, -$coupon->amount);
        }
      }
      return TRUE;
    case 'settings':
      $form['uc_coupon_pane_description'] = array(
        '#type' => 'textarea',
        '#title' => t('Checkout pane message'),
        '#default_value' => variable_get('uc_coupon_pane_description', 'Enter a coupon code for this order.'),
      );
      return $form;
  }
}