You are here

function uc_discounts_apply in Ubercart Discounts (Alternative) 6.2

Same name and namespace in other branches
  1. 7.2 uc_discounts/uc_discounts.module \uc_discounts_apply()

Applies the discounts to an order by creating the necessary line items.

Parameters

$order The order object with any codes set in the uc_discounts_code property:

$compare_to_existing Compare new discounts to existing order discounts and fail if they have changed?:

$save_uses Save the discounts to the uses table? If not done here it will need to be done when the order is successfully created.:

Return value

array An array with the keys: 'success' => @bool, 'message' => @str

2 calls to uc_discounts_apply()
uc_discounts_order in uc_discounts/uc_discounts.module
Implementation of hook_order().
uc_discounts_order_pane_callback in uc_discounts/uc_discounts.module
Callback from hook_order_pane

File

uc_discounts/uc_discounts.module, line 2013

Code

function uc_discounts_apply($order, $save_uses = TRUE, $compare_to_existing = TRUE) {

  // Store existing discount amounts
  $existing_discount_amounts = array();
  foreach (get_existing_discount_line_items($order) as $line_item) {
    $existing_discount_amounts[] = uc_currency_format($line_item["amount"]);
  }

  // Regenerate discount amounts
  $errors = array();
  $warnings = array();
  $messages = array();
  $discounts = get_discounts_for_order($order, $errors, $warnings, $messages);
  foreach ($warnings as $warning) {
    drupal_set_message($warning, 'error');
  }

  // If there were errors, print and return FALSE
  if (!empty($errors)) {
    uc_order_log_changes($order->order_id, $errors);
    foreach ($errors as $error) {
      drupal_set_message($error, 'error');
    }
    return array(
      'success' => FALSE,
      'message' => t('Discounts have changed.  Please review your cart and continue checkout.'),
    );
  }

  // Add discount line items to order
  add_discount_line_items_to_order($order, $discounts);
  $new_discount_amounts = array();
  foreach ($order->uc_discounts_line_items as $line_item) {
    $new_discount_amounts[] = uc_currency_format($line_item["amount"]);
  }
  if ($compare_to_existing) {
    $discount_intersection = array_intersect($existing_discount_amounts, $new_discount_amounts);
    if (count($discount_intersection) != count($existing_discount_amounts)) {

      // Save new discount line items
      $order->uc_discounts_line_items_need_updating = TRUE;
      uc_discounts_order('save', $order, NULL);
      return array(
        'success' => FALSE,
        'message' => t('Discounts have changed.  Please review your cart and continue checkout.'),
      );
    }
  }
  else {
    $order->uc_discounts_line_items_need_updating = TRUE;
    uc_discounts_order('save', $order, NULL);
  }
  if ($save_uses) {
    uc_discounts_uses_save_for_order($order);
  }
  uc_order_log_changes($order->order_id, $messages);
  return array(
    'success' => TRUE,
    'message' => t('Discount code(s) applied to order.'),
  );
}