function commerce_coupon_code_is_valid in Commerce Coupon 7
Checks if a given coupon is valid for a given order. The validation is done by the rules engine.
@TODO: We can throw exception with error message, instead of set the error message to a global variable.
Parameters
$code: The coupon code to validate.
$order: The order at against the $code should be validated.
Return value
boolean Returns TRUE if the code is valid else FALSE.
3 calls to commerce_coupon_code_is_valid()
- commerce_coupon_handler_area_cart_form_validate in includes/
views/ handlers/ commerce_coupon_handler_area_cart_form.inc - Validate: function commerce_coupon_handler_area_cart_form.
- commerce_coupon_pane_checkout_form in includes/
commerce_coupon.checkout_pane.inc - Payment pane: form callback.
- commerce_coupon_pane_checkout_form_validate in includes/
commerce_coupon.checkout_pane.inc - Check whether the coupon code is valid.
File
- ./
commerce_coupon.module, line 223 - Coupon System for Drupal Commerce.
Code
function commerce_coupon_code_is_valid($code, $order) {
global $_commerce_coupon_validation_error_message;
// Trim trailing spaces
$code = trim($code);
$coupon = commerce_coupon_load_by_code($code);
// if no such coupon found, the $code is invalid
if (!is_object($coupon)) {
// $commerce_coupon_validation_error_message = t('Coupon code is invalid.');
return FALSE;
}
// Check if the coupon is already applied to this order. In any case we
// never want that a coupon is applied twice.
if (commerce_coupon_code_is_in_order($code, $order)) {
$_commerce_coupon_validation_error_message = t('Sorry, you can only apply a coupon once per order.');
return FALSE;
}
// Check coupon status.
if ($coupon->is_active == FALSE) {
return FALSE;
}
// We use the drupal_static function to generate a global variable.
// We set per default the result to TRUE. The rules can modify this values,
// by invoking the valid or invalid action.
$validation_results =& drupal_static('commerce_coupon_action_validation_results');
$validation_results = TRUE;
// We invoke the rules. The defined action callback methods sets then the
// validation result appropriate.
rules_invoke_event('commerce_coupon_validate', $coupon, $order);
// We get our global variable and return the result.
return drupal_static('commerce_coupon_action_validation_results');
}