function commerce_gc_submit_order_form_giftcard_transactions in Commerce GC 7
1 string reference to 'commerce_gc_submit_order_form_giftcard_transactions'
File
- ./
commerce_gc.module, line 908 - Provides Giftcard coupon bundle, Giftcard Transaction entity and basic user interface elements.
Code
function commerce_gc_submit_order_form_giftcard_transactions(&$form, &$form_state) {
$order = $form_state['commerce_order'];
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$save = FALSE;
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
if ($line_item_wrapper->type
->value() == 'giftcard_use') {
// Remember which giftcards are still present.
$coupon_id = $line_item_wrapper->commerce_giftcard->coupon_id
->value();
$existing_coupon_ids[$coupon_id] = TRUE;
// Compute the amount that should be written for this transaction by using
// the original value as an offset.
$unit_price_amount = $line_item_wrapper->commerce_unit_price->amount
->value();
if (isset($form_state['original_gc_line_items'][$coupon_id]['amount'])) {
$offset_amount = $form_state['original_gc_line_items'][$coupon_id]['amount'];
$transaction_amount = -($offset_amount - $unit_price_amount);
}
else {
$transaction_amount = $unit_price_amount;
}
// As long as the order is not a shopping cart, record a transaction.
// Shopping cart orders go through a separate refresh process and do not
// create transactions for their giftcard use line items until checkout is
// complete.
if ($transaction_amount && !commerce_cart_order_is_cart($order)) {
commerce_gc_transaction_write($coupon_id, $transaction_amount);
}
// Add the coupon to the order if it is not there already.
if (!in_array($coupon_id, $order_wrapper->commerce_coupons
->raw())) {
$order_wrapper->commerce_coupons[] = $coupon_id;
$save = TRUE;
}
}
}
$remove_coupon_ids = array();
// Handle giftcard line items that have been removed.
foreach ($form_state['original_gc_line_items'] as $coupon_id => $original_line_item_data) {
if (!isset($existing_coupon_ids[$coupon_id])) {
// Create a transaction to reverse the missing line item's total, unless
// the line item was never associated with a transaction.
if (!commerce_cart_order_is_cart($order)) {
commerce_gc_transaction_write($coupon_id, -$original_line_item_data['amount']);
}
$remove_coupon_ids[] = $coupon_id;
}
}
// Remove giftcard coupon from order if giftcard's related line item was
// removed.
foreach ($order_wrapper->commerce_coupons
->raw() as $delta => $coupon_id) {
if (in_array($coupon_id, $remove_coupon_ids)) {
$order_wrapper->commerce_coupons
->offsetUnset($delta);
$save = TRUE;
}
}
if ($save) {
commerce_order_save($order);
}
}