You are here

function uc_coupon_purchase_create in Ubercart Discount Coupons 5

Same name and namespace in other branches
  1. 6 uc_coupon_purchase/uc_coupon_purchase.module \uc_coupon_purchase_create()
  2. 7.3 uc_coupon_purchase/uc_coupon_purchase.module \uc_coupon_purchase_create()
  3. 7.2 uc_coupon_purchase/uc_coupon_purchase.module \uc_coupon_purchase_create()
1 call to uc_coupon_purchase_create()
uc_coupon_purchase_order in uc_coupon_purchase/uc_coupon_purchase.module
Implementation of hook_order().

File

uc_coupon_purchase/uc_coupon_purchase.module, line 266

Code

function uc_coupon_purchase_create($coupon, $qty, $order) {
  global $user;
  $purchaser = $order->billing_company ? $order->billing_company : $order->billing_first_name . ' ' . $order->billing_last_name;
  $coupon->name .= ' ' . t('purchased by !name', array(
    '!name' => $purchaser,
  ));
  $coupon->data['order_id'] = $order->order_id;

  // Generate a new random code and bulk seed.
  $coupon->code = strtoupper(user_password());
  $coupon->bulk_seed = md5(uniqid());

  // If the quantity purchased was more than 1, convert to a bulk coupon.
  if ($qty > 1) {
    $coupon->bulk = TRUE;
    if (!isset($coupon->data['bulk_length'])) {
      $coupon->data['bulk_length'] = 8;
    }
    if (!isset($coupon->data['bulk_number'])) {
      $coupon->data['bulk_number'] = 1;
    }
    $coupon->data['bulk_number'] *= $qty;
  }

  // Create coupon and save order comment.
  db_query("INSERT INTO {uc_coupons} (name, code, value, type, status, valid_from, valid_until, max_uses, minimum_order, data, bulk, bulk_seed) VALUES ('%s', '%s', %f, '%s', 1, %d, %d, %d, %f, '%s', %d, '%s')", $coupon->name, $coupon->code, $coupon->value, $coupon->type, $coupon->valid_from, $coupon->valid_until, $coupon->max_uses, $coupon->minimum_order, serialize($coupon->data), $coupon->bulk, $coupon->bulk_seed);
  uc_order_comment_save($order->order_id, $user->uid, t('Coupon created with code %code.', array(
    '%code' => $coupon->code,
  )));

  // Email coupon details.
  if (module_exists('uc_notify')) {
    if ($coupon->bulk) {
      $subject = variable_get('uc_coupon_purchase_bulk_subject', uc_get_message('uc_coupon_purchase_bulk_subject'));
      $body = variable_get('uc_coupon_purchase_bulk_message', uc_get_message('uc_coupon_purchase_bulk_message'));
      $body = check_markup($body, variable_get('uc_coupon_purchase_bulk_format', FILTER_FORMAT_DEFAULT), FALSE);
    }
    else {
      $subject = variable_get('uc_coupon_purchase_single_subject', uc_get_message('uc_coupon_purchase_single_subject'));
      $body = variable_get('uc_coupon_purchase_single_message', uc_get_message('uc_coupon_purchase_single_message'));
      $body = check_markup($body, variable_get('uc_coupon_purchase_single_format', FILTER_FORMAT_DEFAULT), FALSE);
    }
    $token_filters = array(
      'global' => NULL,
      'user' => $user,
      'order' => $order,
      'uc_coupon' => $coupon,
    );
    $subject = token_replace_multiple($subject, $token_filters);
    $body = token_replace_multiple($body, $token_filters);
    drupal_mail('uc_coupon_purchase', $order->primary_email, $subject, $body, uc_store_email_from(), uc_notify_headers());
  }
}