You are here

function uc_discounts_apply in Ubercart Discounts (Alternative) 7.2

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

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

Parameters

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

bool $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.

bool $compare_to_existing: Compare new discounts to existing order discounts and stop checkout if they do not match.

Return value

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

2 calls to uc_discounts_apply()
uc_discounts_uc_checkout_pane_discounts in uc_discounts/uc_discounts.module
Discounts checkout pane callback.
uc_discounts_uc_order in uc_discounts/uc_discounts.module
Implements hook_uc_order().

File

uc_discounts/uc_discounts.module, line 2259

Code

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

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

  // Regenerate discount amounts.
  $results = uc_discounts_get_discounts_for_order($order, TRUE);
  foreach ($results['messages']['warnings'] as $warning) {
    drupal_set_message($warning, 'error');
  }

  // If there were errors then show on screen and return FALSE.
  // @todo - This is pointless as uc_discounts_get_discounts_for_order does not
  // save any errors.
  if (!empty($results['messages']['errors'])) {
    uc_order_log_changes($order->order_id, $results['messages']['errors']);
    foreach ($results['messages']['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.
  uc_discounts_add_line_items_to_order($order, $results['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_uc_order('save', $order, NULL);
      return array(
        'success' => FALSE,
        'message' => t('Discounts have changed. Please review your cart and continue checkout.'),
      );
    }
  }
  else {

    // @todo Should this be FALSE?
    $order->uc_discounts_line_items_need_updating = TRUE;
    uc_discounts_uc_order('save', $order, NULL);
  }
  if ($save_uses) {
    uc_discounts_uses_save_for_order($order);
  }
  if (!empty($results['messages']['success'])) {
    uc_order_log_changes($order->order_id, $results['messages']['success']);
  }
  return array(
    'success' => TRUE,
    'message' => t('Discount code(s) applied to order.'),
  );
}