function commerce_coupon_handler_area_cart_form_validate in Commerce Coupon 7.2
Same name and namespace in other branches
- 7 includes/views/handlers/commerce_coupon_handler_area_cart_form.inc \commerce_coupon_handler_area_cart_form_validate()
Validate: function commerce_coupon_handler_area_cart_form
1 string reference to 'commerce_coupon_handler_area_cart_form_validate'
- commerce_coupon_handler_area_cart_form::views_form in includes/
views/ handlers/ commerce_coupon_handler_area_cart_form.inc
File
- includes/
views/ handlers/ commerce_coupon_handler_area_cart_form.inc, line 138 - Display a commerce coupon form field on the cart form.
Code
function commerce_coupon_handler_area_cart_form_validate($form, $form_state) {
// Get the form and values for the coupon form.
$coupon_array_parents = array_slice($form_state['triggering_element']['#array_parents'], 0, -1);
$coupon_parents = array_slice($form_state['triggering_element']['#parents'], 0, -1);
$coupon_values = drupal_array_get_nested_value($form_state['values'], $coupon_parents);
$coupon_form = drupal_array_get_nested_value($form, $coupon_array_parents);
$coupon_code = $coupon_values['coupon_code'];
$order = $form_state['order'];
$error = '';
// No code provided
if (empty($coupon_code)) {
$error = t('Please enter a coupon code.');
}
else {
// Check if the coupon code has already been applied.
$coupon = commerce_coupon_load_by_code($coupon_code);
if (empty($coupon)) {
$error = t('Please enter a valid coupon code.');
}
else {
// The same coupon cannot be added twice.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_coupons as $order_coupon_wrapper) {
if ($order_coupon_wrapper->coupon_id
->value() == $coupon->coupon_id) {
$error = t('The coupon you have entered has already been applied to your order');
}
}
}
}
// If a coupon was invalidated during the cart refresh (e.g. if its
// discounts failed their conditions), an error message will have been
// set.
if (empty($error)) {
$error =& drupal_static('commerce_coupon_error_' . strtolower($coupon_code));
}
// If we have errors set the form error.
if (!empty($error)) {
form_set_error(implode('][', $coupon_form['coupon_code']['#parents']), $error);
}
}