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 aada39d..c306826 100644 --- a/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc +++ b/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc @@ -79,6 +79,9 @@ 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'; + $form[$this->options['id']] = array( '#prefix' => '
', '#suffix' => '
', @@ -100,18 +103,8 @@ class commerce_coupon_handler_area_cart_form extends views_handler_area { '#submit' => array('commerce_coupon_handler_area_cart_form_submit'), ); // Attach ajax if views ajax enabled. - // NOTE: shortcircuting this as ajax isn't working yet. - if (FALSE && $this->view->use_ajax) { - $form[$this->options['id']]['coupon_add']['#ajax'] = array( - 'callback' => 'commerce_coupon_cart_add_coupon_callback', - 'wrapper' => 'commerce-coupon-cart-form-wrapper', - ); - // Placeholder for any callback validation errors. - $form[$this->options['id']]['status_messages'] = array( - '#type' => 'markup', - '#markup' => '
', - '#weight' => -1, - ); + if ($this->view->use_ajax) { + // @todo implement AJAX. } // First look for an order_id argument. @@ -144,7 +137,12 @@ class commerce_coupon_handler_area_cart_form extends views_handler_area { * Validate: function commerce_coupon_handler_area_cart_form */ function commerce_coupon_handler_area_cart_form_validate($form, $form_state) { - $coupon_code = $form_state['values']['coupon_code']; + // 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 = ''; @@ -179,7 +177,7 @@ function commerce_coupon_handler_area_cart_form_validate($form, $form_state) { } // If we have errors set the form error. if (!empty($error)) { - form_set_error('coupon_code', $error); + form_set_error(implode('][', $coupon_form['coupon_code']['#parents']), $error); } } @@ -190,7 +188,10 @@ function commerce_coupon_handler_area_cart_form_validate($form, $form_state) { * redemption? */ function commerce_coupon_handler_area_cart_form_submit($form, $form_state) { - $coupon_code = $form_state['values']['coupon_code']; + // Get the values for the coupon form. + $coupon_parents = array_slice($form_state['triggering_element']['#parents'], 0, -1); + $coupon_values = drupal_array_get_nested_value($form_state['values'], $coupon_parents); + $coupon_code = $coupon_values['coupon_code']; $order = $form_state['order']; $error = ''; @@ -212,46 +213,3 @@ function commerce_coupon_handler_area_cart_form_submit($form, $form_state) { rules_invoke_all('commerce_coupon_applied_to_cart', $coupon, $order); } } - -/** - * Ajax callback: coupon add button. - * - * @todo This logic is not working yet. - */ -function commerce_coupon_cart_add_coupon_callback($form, &$form_state) { - // We only execute this callback if the triggering element is the add to cart - // button. - if (!isset($form_state['triggering_element']) || $form_state['triggering_element']['#name'] != 'coupon_add') { - return; - } - - $coupon_code = $form_state['input']['coupon_code']; - $order = $form_state['order']; - - if ($errors = form_get_error('coupon_code')) { - drupal_set_message('testing'); - } - else { - // Reload the order so it is not out of date. - $order = commerce_order_load($order->order_id); - - // Recalculate discounts. - commerce_cart_order_refresh($order); - - // Re-render the cart form. - // This will also cause the coupon form and coupon summary view to re-render. - $view_id = variable_get('commerce_cart_view_override_page_view', 'commerce_cart_form'); - $display_id = 'default'; - // Generate a class to work with the ajax replace command. - // May also want to make this a view option in situations where views does not - // generate default classes. - $view_ajax_id = '.view-id-' . $view_id; - // Load the rendered view and ajax replace it. - $cart_form = commerce_embed_view($view_id, $display_id, array($order->order_id)); - $commands[] = ajax_command_replace($view_ajax_id, $cart_form); - } - - $commands[] = ajax_command_replace('#commerce-coupon-form-errors', theme('status_messages')); - - return array('#type' => 'ajax', '#commands' => $commands); -}