You are here

function uc_coupon_purchase_uc_order in Ubercart Discount Coupons 7.3

Same name and namespace in other branches
  1. 7.2 uc_coupon_purchase/uc_coupon_purchase.module \uc_coupon_purchase_uc_order()

Implements hook_uc_order().

File

uc_coupon_purchase/uc_coupon_purchase.module, line 344

Code

function uc_coupon_purchase_uc_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($order->uid)) !== FALSE) {
        foreach ($order->products as $product) {
          $result = db_query("SELECT * FROM {uc_coupon_purchase} WHERE nid = :nid", array(
            ':nid' => $product->nid,
          ));
          foreach ($result as $row) {
            if ($row->model == $product->model || empty($row->model)) {
              $coupon = uc_coupon_load($row->base_cid);

              // Append purchaser data to the coupon name.
              $purchaser = trim(!empty($order->billing_company) ? $order->billing_company : $order->billing_first_name . ' ' . $order->billing_last_name);
              if (empty($purchaser)) {
                $purchaser = "Anonymous";
              }
              $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, $product);
              rules_invoke_event('uc_coupon_purchase', $order, $new_coupon, $product);
            }
          }
        }
      }
      break;
  }
}