You are here

function commerce_coupon_usage_transaction_write in Commerce Coupon 7.2

Write a new commerce coupon usage transaction record

Parameters

$coupon_id: A coupon id.

$order_id: An order id.

null $uid: A user id.

int $date: A date.

Return value

int|null A transaction id or NULL.

4 calls to commerce_coupon_usage_transaction_write()
commerce_coupon_usage_commerce_coupon_final_checkout_validate in modules/usage/commerce_coupon_usage.module
Implements hook_commerce_coupon_final_checkout_validate().
commerce_coupon_usage_commerce_order_insert in modules/usage/commerce_coupon_usage.module
Implements hook_commerce_order_insert().
commerce_coupon_usage_commerce_order_update in modules/usage/commerce_coupon_usage.module
Implements hook_commerce_order_update().
commerce_coupon_usage_record_usage in modules/usage/commerce_coupon_usage.module
Record coupon usage for an order.

File

modules/usage/commerce_coupon_usage.module, line 215

Code

function commerce_coupon_usage_transaction_write($coupon_id, $order_id, $uid = NULL, $date = 0) {
  $values = array(
    'coupon_id' => $coupon_id,
    'order_id' => $order_id,
    'date' => $date ? $date : REQUEST_TIME,
  );
  if ($uid) {
    $values['uid'] = $uid;
  }
  else {

    // Otherwise try to find a user from the order
    $order = commerce_order_load($order_id);
    if ($order->uid) {
      $values['uid'] = $order->uid;
    }
    else {
      $values['uid'] = 0;
    }
  }
  $transaction = commerce_coupon_usage_transaction_new($values);

  // Insert record
  commerce_coupon_usage_transaction_save($transaction);
  return !empty($transaction->transaction_id) ? $transaction->transaction_id : NULL;
}