function uc_discounts_js_calculate in Ubercart Discounts (Alternative) 6.2
AJAX callback for discounts calculation.
Calculate discount for an order in the checkout page.
1 string reference to 'uc_discounts_js_calculate'
- uc_discounts_menu in uc_discounts/
uc_discounts.module - Implementation of hook_menu().
File
- uc_discounts/
uc_discounts.module, line 648
Code
function uc_discounts_js_calculate() {
global $user;
if (!empty($_SESSION["cart_order"])) {
$order_id = $_SESSION['cart_order'];
$order = uc_order_load($order_id);
//If session order exists, use it
if (is_null($order)) {
print '{}';
exit;
}
}
else {
$order = new stdClass();
$order->uid = $user->uid;
$order->products = uc_cart_get_contents();
}
$order->uc_discounts_codes = uc_discounts_codes_to_array($_POST['uc-discounts-codes']);
drupal_alter('uc_discounts_codes', $order, 'js_calculate');
$line_items = array();
$errors = array();
$warnings = array();
$messages = array();
$discounts = get_discounts_for_order($order, $errors, $warnings, $messages);
$i = 0;
// Session vars get used by conditional action
$_SESSION['uc_discounts_codes'] = $order->uc_discounts_codes;
foreach ($discounts as $discount) {
if ($discount->amount != 0) {
$line_item = array();
$line_item["id"] = LINE_ITEM_KEY_NAME . $i++;
$line_item["type"] = $discount->type;
$line_item["title"] = $discount->title;
$line_item["amount"] = -$discount->amount;
$line_item["weight"] = $discount->weight;
$line_items[] = $line_item;
}
}
if (!empty($warnings)) {
$warnings2 = array();
foreach ($warnings as $warning) {
$warnings2[] = t('Warning: @warning', array(
'@warning' => $warning,
));
}
$errors = array_merge($errors, $warnings2);
}
$calculate_discount_response = array(
CALCULATE_DISCOUNT_RESPONSE_LINE_ITEMS_KEY => $line_items,
CALCULATE_DISCOUNT_RESPONSE_ERRORS_KEY => $errors,
CALCULATE_DISCOUNT_RESPONSE_MESSAGES_KEY => $messages,
);
drupal_json($calculate_discount_response);
exit;
}