You are here

function uc_coupon_purchase_order in Ubercart Discount Coupons 6

Same name and namespace in other branches
  1. 5 uc_coupon_purchase/uc_coupon_purchase.module \uc_coupon_purchase_order()

Implementation of hook_order().

File

uc_coupon_purchase/uc_coupon_purchase.module, line 326

Code

function uc_coupon_purchase_order($op, $order, $status) {
  switch ($op) {
    case 'update':
      if ($status == variable_get('uc_coupon_purchase_order_status', 'completed') && $order->order_status != $status && $order->uid > 0 && ($order_user = user_load(array(
        'uid' => $order->uid,
      ))) !== FALSE) {
        foreach ($order->products as $product) {
          $result = db_query("SELECT * FROM {uc_coupon_purchase} WHERE nid = %d", $product->nid);
          while ($row = db_fetch_object($result)) {
            if ($row->model == $product->model || empty($row->model)) {
              $coupon = uc_coupon_load($row->base_cid);

              // Append purchaser data to the coupon name.
              $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;

              // Create new coupon and link to purchaser.
              $new_coupon = uc_coupon_purchase_create($coupon, $product->qty, $order->uid);
              uc_order_comment_save($order->order_id, 0, t('Coupon created with code %code.', array(
                '%code' => $new_coupon->code,
              )));

              // Fire hook and conditional actions to send emails for this coupon purchase.
              module_invoke_all('uc_coupon_purchase', $order, $new_coupon);
              ca_pull_trigger('uc_coupon_purchase', $order, $new_coupon);
            }
          }
        }
      }
      break;
  }
}