You are here

function uc_discounts_uses_save_for_order in Ubercart Discounts (Alternative) 7.2

Same name and namespace in other branches
  1. 6.2 uc_discounts/uc_discounts.module \uc_discounts_uses_save_for_order()

Records uses of a discount for an order.

Parameters

object $order:

2 calls to uc_discounts_uses_save_for_order()
uc_discounts_apply in uc_discounts/uc_discounts.module
Applies the discounts to an order by creating the necessary line items.
uc_discounts_uc_checkout_complete in uc_discounts/uc_discounts.module
Implements hook_uc_checkout_complete().

File

uc_discounts/uc_discounts.module, line 2208

Code

function uc_discounts_uses_save_for_order($order) {
  $results = uc_discounts_get_discounts_for_order($order);

  // Delete existing discount uses for order.
  uc_discounts_uses_delete_for_order($order->order_id);

  // Insert current discount uses.
  foreach ($results['discounts'] as $discount) {
    $code = !empty($discount->code) ? $discount->code : '';
    $times_applied = is_numeric($discount->times_applied) ? $discount->times_applied : 1;
    $amount = is_numeric($discount->amount) ? $discount->amount : 0;
    $discount_use = new stdClass();
    $discount_use->discount_id = $discount->discount_id;
    $discount_use->user_id = $order->uid;
    $discount_use->order_id = $order->order_id;
    $discount_use->code = $code;
    $discount_use->times_applied = $times_applied;
    $discount_use->amount = $amount;
    $discount_use->insert_timestamp = REQUEST_TIME;
    drupal_write_record('uc_discounts_uses', $discount_use);
  }
}