function uc_coupon_uc_order in Ubercart Discount Coupons 7.2
Same name and namespace in other branches
- 7.3 uc_coupon.module \uc_coupon_uc_order()
Implements hook_uc_order().
This makes sure the active, validated coupons are saved to the order. It also prevents any fake coupon cart-items from being saved to the order.
File
- ./
uc_coupon.module, line 1276
Code
function uc_coupon_uc_order($op, &$order) {
if ($op == 'presave') {
// Apply any session coupons to the current cart order.
if (_uc_coupon_is_checkout_order($order)) {
$coupons = uc_coupon_session_validate();
// Index existing line items by coupon code.
$items = array();
foreach ($order->line_items as $index => $line) {
if ($line['type'] == 'coupon') {
// For orders created before multi-coupons were enabled, the code was not saved with the line item.
// In this case, we retreive it from the uc_coupons_orders table.
$code = isset($line['data']['code']) ? $line['data']['code'] : db_query('SELECT code FROM {uc_coupons_orders} WHERE oid = :oid', array(
':oid' => $order->order_id,
))
->fetchField();
$items[$code] = $index;
}
}
// Index existing entries in {uc_coupons_orders} by coupon code.
$entries = db_query('SELECT code, cuid FROM {uc_coupons_orders} WHERE oid = :oid', array(
':oid' => $order->order_id,
))
->fetchAllKeyed(0, 1);
// Update, insert or delete line items and entries in uc_coupons_orders.
$insert = array();
$order->data['coupons'] = array();
foreach ($coupons as $coupon) {
$order->data['coupons'][$coupon->code] = $coupon->discounts;
// Handle entries in {uc_coupons_orders}.
if (isset($entries[$coupon->code])) {
db_update('uc_coupons_orders')
->condition('cuid', $entries[$coupon->code])
->fields(array(
'cid' => $coupon->cid,
'value' => $coupon->amount,
))
->execute();
unset($entries[$coupon->code]);
}
else {
$insert[] = array(
$coupon->cid,
$order->order_id,
$coupon->code,
$coupon->value,
);
}
// Handle line items.
if (isset($items[$coupon->code])) {
$line =& $order->line_items[$items[$coupon->code]];
$line['title'] = $coupon->title;
$line['amount'] = -$coupon->pretax_amount;
$line['data']['code'] = $coupon->code;
uc_order_update_line_item($line['line_item_id'], $line['title'], $line['amount'], $line['data']);
unset($items[$coupon->code]);
}
else {
// Create a new line item.
$order->line_items[] = uc_order_line_item_add($order->order_id, 'coupon', $coupon->title, -$coupon->pretax_amount, _uc_line_item_data('coupon', 'weight'), array(
'code' => $coupon->code,
));
}
}
// Insert new entries in {uc_coupons_orders}
if (!empty($insert)) {
$query = db_insert('uc_coupons_orders');
$query
->fields(array(
'cid',
'oid',
'code',
'value',
));
foreach ($insert as $fields) {
$query
->values($fields);
}
$query
->execute();
}
// Delete orphaned entries in {uc_coupons_orders}
if (!empty($entries)) {
db_delete('uc_coupons_orders')
->condition('cuid', $entries)
->execute();
}
// Remove orphaned line-items.
foreach ($items as $index) {
uc_order_delete_line_item($order->line_items[$index]['line_item_id']);
unset($order->line_items[$index]);
}
usort($order->line_items, 'uc_weight_sort');
}
// Make sure any fake cart items don't get saved with the order if the checkout page is skipped
// (e.g. Paypal Express Checkout, Google Checkout)
foreach ($order->products as $key => $product) {
if (isset($product->module) && $product->module == 'uc_coupon') {
unset($order->products[$key]);
}
}
}
}