diff --git a/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc b/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc index 1659703..b393dc3 100644 --- a/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc +++ b/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc @@ -13,6 +13,7 @@ class commerce_coupon_handler_area_cart_form extends views_handler_area { function option_definition() { $options = parent::option_definition(); + $options['coupon_cart_form_view']['default'] = 'order_coupon_list|checkout'; $options['weight']['default'] = 99; return $options; @@ -25,6 +26,27 @@ class commerce_coupon_handler_area_cart_form extends views_handler_area { parent::options_form($form, $form_state); unset($form['empty']); + // Build an options array of Views available for the cart contents pane. + $options = array(); + + // Generate an option list from all user defined and module defined views. + foreach (views_get_all_views() as $view_id => $view_value) { + // Only include line item Views. + if ($view_value->base_table == 'commerce_coupon') { + foreach ($view_value->display as $display_id => $display_value) { + $options[check_plain($view_id)][$view_id . '|' . $display_id] = check_plain($display_value->display_title); + } + } + } + + $form['coupon_cart_form_view'] = array( + '#type' => 'select', + '#title' => t('Coupons Cart View'), + '#description' => t('Specify the View to render the cart summary.'), + '#options' => array('none' => t('None')) + $options, + '#default_value' => $this->options['coupon_cart_form_view'], + ); + $form['weight'] = array( '#type' => 'textfield', '#title' => t('Form item weight'), @@ -55,7 +77,6 @@ class commerce_coupon_handler_area_cart_form extends views_handler_area { } function views_form(&$form, &$form_state) { - // Ensure this include file is loaded when the form is rebuilt from the cache. $form_state['build_info']['files']['coupon_cart_form'] = drupal_get_path('module', 'commerce_coupon') . '/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc'; @@ -76,7 +97,9 @@ class commerce_coupon_handler_area_cart_form extends views_handler_area { '#value' => t('Add coupon'), '#name' => 'coupon_add', // Limit validation to the coupon code. - '#limit_validation_errors' => array(array('coupon_code')), + '#limit_validation_errors' => array( + array('cart_contents_form', 'cart_contents_form_view', $this->options['id'], 'coupon_code') + ), '#validate' => array('commerce_coupon_handler_area_cart_form_validate'), '#submit' => array('commerce_coupon_handler_area_cart_form_submit'), ); @@ -96,6 +119,18 @@ class commerce_coupon_handler_area_cart_form extends views_handler_area { } } $order = !empty($order_id) ? commerce_order_load($order_id) : commerce_cart_order_load($GLOBALS['user']->uid); + + // Extract the View and display keys from the cart contents pane setting. + $coupon_summary_view = $this->options['coupon_cart_form_view']; + if ($coupon_summary_view != 'none') { + list($view_id, $display_id) = explode('|', $coupon_summary_view); + if (!empty($view_id) && !empty($display_id) && views_get_view($view_id)) { + $form[$this->options['id']]['redeemed_coupons'] = array( + '#type' => 'markup', + '#markup' => commerce_embed_view($view_id, $display_id, array($order->order_id)), + ); + } + } } } @@ -141,8 +176,14 @@ function commerce_coupon_handler_area_cart_form_validate($form, $form_state) { if (empty($error)) { $error = &drupal_static('commerce_coupon_error_' . strtolower($coupon_code)); } - // If we have errors set the form error. - if (!empty($error)) { + + if (empty($error)) { + // Allow modules/rules to act when a coupon has been successfully added + // to the cart. + rules_invoke_all('commerce_coupon_applied_to_cart', $coupon, $order); + } + else { + // If we have errors set the form error. form_set_error(implode('][', $coupon_form['coupon_code']['#parents']), $error); } }