function commerce_gc_line_item_add_form_submit in Commerce GC 7
File
- ./
commerce_gc.module, line 564 - Provides Giftcard coupon bundle, Giftcard Transaction entity and basic user interface elements.
Code
function commerce_gc_line_item_add_form_submit($line_item, $element, &$form_state, $form) {
// No need to go further if there are errors.
if (form_get_errors()) {
return;
}
// Make sure amount is either empty or a number greater than zero.
$decimal_amount = $element['actions']['amount']['#value'];
if ($decimal_amount && (!is_numeric($decimal_amount) || $decimal_amount < 0)) {
return t('You have entered an invalid amount.');
}
$amount = commerce_currency_decimal_to_amount($decimal_amount, commerce_default_currency());
// Make sure the giftcard exists.
$code = $element['actions']['code']['#value'];
$coupon = commerce_coupon_load_by_code($code);
if (!$coupon || $coupon->type != 'giftcard_coupon') {
return t('This giftcard does not exist.');
}
// Make sure there is not already a giftcard use line item referencing this
// code.
$order = $form_state['commerce_order'];
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_line_items as $order_line_item_wrapper) {
if ($order_line_item_wrapper->type
->value() == 'giftcard_use' && $order_line_item_wrapper->commerce_giftcard->coupon_id
->value() == $coupon->coupon_id) {
return t('You may not add the same giftcard code more than once per order.');
}
}
// Make sure it has a positive balance.
$balance = commerce_gc_giftcard_balance($coupon->coupon_id);
if ($balance <= 0) {
return t('This balance on this giftcard is not greater than zero.');
}
// Make sure that the amount entered does not exceed the balance
if ($amount && $amount > $balance) {
$balance_display = commerce_currency_format($balance, commerce_default_currency());
return t('You have entered an amount greater than the balance on this card. Card balance is @balance', array(
'@balance' => $balance_display,
));
}
commerce_order_calculate_total($order);
$order_total = $order_wrapper->commerce_order_total->amount
->value();
$ceiling_amount = $order_total < $balance ? $order_total : $balance;
$line_item_amount = $amount ? $amount : $ceiling_amount;
// This can happen if the order total is zero.
if (!$line_item_amount) {
return t('You cannot add a zero amount giftcard use line item.');
}
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
// Set the giftcard reference
$line_item_wrapper->commerce_giftcard = $coupon;
// Set the price
$price = array(
'amount' => -$line_item_amount,
'currency_code' => commerce_default_currency(),
'data' => array(),
);
$line_item_wrapper->commerce_unit_price->amount = -$line_item_amount;
$line_item_wrapper->commerce_unit_price->currency_code = commerce_default_currency();
$base_price = array(
'amount' => 0,
'currency_code' => commerce_default_currency(),
'data' => array(),
);
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($base_price, $component_title, $price, TRUE);
}