diff --git a/includes/commerce_coupon.checkout_pane.inc b/includes/commerce_coupon.checkout_pane.inc
index 7383ca8..c0b44ab 100644
--- a/includes/commerce_coupon.checkout_pane.inc
+++ b/includes/commerce_coupon.checkout_pane.inc
@@ -12,95 +12,110 @@
* Payment pane: form callback.
*/
function commerce_coupon_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
- // Allow to replace pane content with ajax calls.
- $pane_form = array(
- '#prefix' => '
',
- '#suffix' => '
',
- );
- // Store the payment methods in the form for validation purposes.
- $pane_form['coupon_code'] = array(
- '#type' => 'textfield',
- '#title' => t('Coupon Code'),
- '#description' => t('Enter here your coupon code.'),
- );
+ $query = new EntityFieldQuery();
+ // Get all active commerce coupons
+ $result = $query->entityCondition('entity_type', 'commerce_coupon')
+ ->propertyCondition('is_active', 1, "=")
+ ->execute();
+ // Do the work if there are any active commerce coupons
+ if (!empty($result)) {
+
+ // Allow to replace pane content with ajax calls.
+ $pane_form = array(
+ '#prefix' => '',
+ '#suffix' => '
',
+ );
- if (variable_get('commerce_coupon_checkout_enable_add_button', TRUE)) {
- $pane_form['coupon_add'] = array(
- '#type' => 'button',
- '#value' => t('Add coupon'),
- '#name' => 'coupon_add',
- '#limit_validation_errors' => array(),
- '#ajax' => array(
- 'callback' => 'commerce_coupon_add_coupon_callback',
- 'wrapper' => 'commerce-checkout-coupon-ajax-wrapper',
- ),
+ // Store the payment methods in the form for validation purposes.
+ $pane_form['coupon_code'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Coupon Code'),
+ '#description' => t('Enter here your coupon code.'),
);
- }
- // Extract the View and display keys from the cart contents pane setting.
- $pane_view = variable_get('commerce_coupon_checkout_pane_view', 'commerce_coupon_review_pane|checkout');
- if ($pane_view <> 'none') {
- list($view_id, $display_id) = explode('|', $pane_view);
- if (!empty($view_id) && !empty($display_id) && views_get_view($view_id)) {
- $pane_form['redeemed_coupons'] = array(
- '#type' => 'markup',
- '#markup' => commerce_embed_view($view_id, $display_id, array($order->order_id)),
+ if (variable_get('commerce_coupon_checkout_enable_add_button', TRUE)) {
+ $pane_form['coupon_add'] = array(
+ '#type' => 'button',
+ '#value' => t('Add coupon'),
+ '#name' => 'coupon_add',
+ '#limit_validation_errors' => array(),
+ '#ajax' => array(
+ 'callback' => 'commerce_coupon_add_coupon_callback',
+ 'wrapper' => 'commerce-checkout-coupon-ajax-wrapper',
+ ),
);
}
- }
- if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] == 'coupon_add') {
- if (!empty($form_state['values']['commerce_coupon']['coupon_add'])) {
+ // Extract the View and display keys from the cart contents pane setting.
+ $pane_view = variable_get('commerce_coupon_checkout_pane_view', 'commerce_coupon_review_pane|checkout');
+ if ($pane_view <> 'none') {
list($view_id, $display_id) = explode('|', $pane_view);
- if (!empty($view_id) && !empty($display_id)) {
- global $_commerce_coupon_validation_error_message;
- $code = $form_state['input']['commerce_coupon']['coupon_code'];
- if (!empty($code) && commerce_coupon_code_is_valid($code, $order)) {
- $coupon = commerce_coupon_load_by_code($code);
- commerce_coupon_redeem_coupon($coupon, $order);
-
- // Clear the field value so that the coupon code does not get resubmitted
- // causing an error when user uses main "Continue to next step" submit.
- $pane_form['coupon_code']['#value'] = '';
-
- if (isset($order->order_id) && is_numeric($order->order_id)) {
- // This is to get the summary of the form rerendered.
- $alter_coupon_commands = &drupal_static('alter_coupon_commands');
- $coupon_current_order = &drupal_static('coupon_current_order');
- $alter_coupon_commands = TRUE;
- $coupon_current_order = $order->order_id;
- }
- // Force shopping cart refresh to recalculate all prices there,
- // which is not done automatically in case of percentage coupons.
- commerce_cart_order_refresh($order);
- $pane_form['redeemed_coupons']['#markup'] = commerce_embed_view($view_id, $display_id, array($order->order_id));
- }
- else {
- if (isset($_commerce_coupon_validation_error_message)) {
- $message = $_commerce_coupon_validation_error_message;
+ if (!empty($view_id) && !empty($display_id) && views_get_view($view_id)) {
+ $pane_form['redeemed_coupons'] = array(
+ '#type' => 'markup',
+ '#markup' => commerce_embed_view($view_id, $display_id, array($order->order_id)),
+ );
+ }
+ }
+
+ if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] == 'coupon_add') {
+ if (!empty($form_state['values']['commerce_coupon']['coupon_add'])) {
+ list($view_id, $display_id) = explode('|', variable_get('commerce_coupon_review_pane_view', 'commerce_coupon_review_pane|checkout'));
+ if (!empty($view_id) && !empty($display_id)) {
+ global $_commerce_coupon_validation_error_message;
+ $code = $form_state['input']['commerce_coupon']['coupon_code'];
+ if (!empty($code) && commerce_coupon_code_is_valid($code, $order)) {
+ $coupon = commerce_coupon_load_by_code($code);
+ commerce_coupon_redeem_coupon($coupon, $order);
+
+ // Clear the field value so that the coupon code does not get resubmitted
+ // causing an error when user uses main "Continue to next step" submit.
+ $pane_form['coupon_code']['#value'] = '';
+
+ if (isset($order->order_id) && is_numeric($order->order_id)) {
+ // This is to get the summary of the form rerendered.
+ $alter_coupon_commands = &drupal_static('alter_coupon_commands');
+ $coupon_current_order = &drupal_static('coupon_current_order');
+ $alter_coupon_commands = TRUE;
+ $coupon_current_order = $order->order_id;
+ }
+ // Force shopping cart refresh to recalculate all prices there,
+ // which is not done automatically in case of percentage coupons.
+ commerce_cart_order_refresh($order);
+ $pane_form['redeemed_coupons']['#markup'] = commerce_embed_view($view_id, $display_id, array($order->order_id));
}
else {
- $message = t('Your coupon code is not valid.');
+ if (isset($_commerce_coupon_validation_error_message)) {
+ $message = $_commerce_coupon_validation_error_message;
+ }
+ else {
+ $message = t('Your coupon code is not valid.');
+ }
+ form_set_error('commerce_coupon][coupon_code', $message);
}
- form_set_error('commerce_coupon][coupon_code', $message);
}
}
}
- }
- // Display any new status messages added by this pane within the pane's area.
- if (drupal_get_messages(NULL, FALSE)) {
- $pane_form['status_messages'] = array(
- '#type' => 'markup',
- '#markup' => theme('status_messages'),
- '#weight' => -1,
- );
+ // Display any new status messages added by this pane within the pane's area.
+ if (drupal_get_messages(NULL, FALSE)) {
+ $pane_form['status_messages'] = array(
+ '#type' => 'markup',
+ '#markup' => theme('status_messages'),
+ '#weight' => -1,
+ );
+ }
+ }
+ else {
+ // No active coupons available => initiate blank $pane_form
+ $pane_form = '';
}
return $pane_form;
}
+
/**
* Check whether the coupon code is valid.
*/
@@ -169,7 +184,7 @@ function commerce_coupon_ajax_render_alter(&$commands) {
'#markup' => commerce_embed_view($view_id, $display_id, array($coupon_current_order)),
);
$contents = drupal_render($contents);
- $commands[] = ajax_command_replace('.view-id-' . $view_id, $contents);
+ $commands[] = ajax_command_replace('.view-commerce-cart-summary', $contents);
}
}