You are here

function uc_checkout_pane_coupon in Ubercart Discount Coupons 6

Same name and namespace in other branches
  1. 5 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 909
Provides discount coupons for Ubercart.

Code

function uc_checkout_pane_coupon($op, &$arg1, $arg2) {
  switch ($op) {
    case 'view':
      drupal_add_js(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.js');
      drupal_add_js(array(
        'ucURL' => array(
          'applyCoupon' => url('cart/checkout/coupon'),
        ),
      ), 'setting');
      if (isset($_SESSION['uc_coupon'])) {
        $code = $_SESSION['uc_coupon'];
      }
      else {
        if (isset($arg1->data['coupon'])) {
          $code = $_SESSION['uc_coupon'] = $arg1->data['coupon'];
        }
        else {
          $code = '';
        }
      }
      if ($code) {
        $coupon = uc_coupon_validate($code, empty($arg1->products) ? NULL : $arg1);
        if ($coupon->valid) {
          if (variable_get('uc_coupon_show_in_cart', TRUE)) {

            // Modify stored subtotal to ignore coupon cart item.
            drupal_add_js('$(function() {
              if (window.set_line_item) {
                li_values["subtotal"] += ' . $coupon->amount . ';
              }
            });', 'inline');
          }
          drupal_add_js('$(function() {
            if (window.set_line_item) {
              set_line_item("coupon", "' . $coupon->title . '", ' . -$coupon->amount . ', ' . _line_item_data('coupon', 'weight') . ');
            }
          });', 'inline');
        }
      }
      $description = variable_get('uc_coupon_pane_description', t('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' => 'submit',
        '#submit' => array(
          'uc_coupon_uc_cart_checkout_submit',
        ),
        '#value' => t('Apply to order'),
        '#attributes' => array(
          'onclick' => "getCoupon(); return false;",
        ),
        '#suffix' => '<span id="coupon-throbber"></span>',
      );
      return array(
        'description' => $description,
        'contents' => $contents,
        'theme' => 'uc_checkout_pane_coupon',
      );
    case 'process':
      if ($arg2['code']) {
        $arg1->data['coupon'] = $arg2['code'];
        $coupon = uc_coupon_validate($arg1->data['coupon'], $arg1);
        if (!$coupon->valid) {
          drupal_set_message($coupon->message, 'error');
          unset($_SESSION['uc_coupon']);
          unset($arg1->data['coupon']);
          return FALSE;
        }
        $_SESSION['uc_coupon'] = $arg1->data['coupon'];
        _uc_coupon_apply_to_order($arg1->order_id, $coupon);
      }
      elseif (isset($_SESSION['uc_coupon'])) {
        unset($_SESSION['uc_coupon']);
        unset($arg1->data['coupon']);
        _uc_coupon_apply_to_order($arg1->order_id, NULL);
      }
      return TRUE;
    case 'settings':
      $form['uc_coupon_collapse_pane'] = array(
        '#type' => 'checkbox',
        '#title' => t('Collapse checkout pane by default.'),
        '#default_value' => variable_get('uc_coupon_collapse_pane', FALSE),
      );
      $form['uc_coupon_pane_description'] = array(
        '#type' => 'textarea',
        '#title' => t('Checkout pane message'),
        '#default_value' => variable_get('uc_coupon_pane_description', t('Enter a coupon code for this order.')),
      );
      return $form;
  }
}