From 9aed479e126bdd1855afb61249c9f8b39e2ca462 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Sat, 14 Apr 2012 00:46:29 +0200 Subject: [PATCH] Ajax functionality for checkout page (mostly by jucallme) --- includes/commerce_coupon.checkout_pane.inc | 69 ++++++++++++++++++++++++++++ 1 files changed, 69 insertions(+), 0 deletions(-) diff --git a/includes/commerce_coupon.checkout_pane.inc b/includes/commerce_coupon.checkout_pane.inc index f77caa4..63645fc 100644 --- a/includes/commerce_coupon.checkout_pane.inc +++ b/includes/commerce_coupon.checkout_pane.inc @@ -18,15 +18,57 @@ function commerce_coupon_pane_checkout_form($form, &$form_state, $checkout_pane, '#description' => t('Enter here your coupon code.'), ); + $pane_form['coupon_add'] = array( + '#type' => 'button', + '#value' => t('Add coupon'), + '#name' => 'coupon_add', + '#limit_validation_errors' => array(), + '#element_validate' => array(), + '#ajax' => array( + 'callback' => 'commerce_coupon_add_coupon_callback', + 'wrapper' => 'coupon-call-back', + ), + ); + // Extract the View and display keys from the cart contents pane setting. list($view_id, $display_id) = explode('|', variable_get('commerce_coupon_review_pane_view', 'commerce_coupon_review_pane|default')); if (!empty($view_id) && !empty($display_id)) { $pane_form['redeemed_coupons'] = array( '#type' => 'markup', + '#prefix' => '
', + '#suffix' => '
', '#markup' => commerce_embed_view($view_id, $display_id, array($order->order_id)), ); } + if ($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|default')); + if (!empty($view_id) && !empty($display_id)) { + $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 value, as we dont want errors when the user hits submit on fnal check out, as we would try add the value in the field again. + $form['commerce_coupon']['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; + } + $pane_form['redeemed_coupons']['#markup'] = commerce_embed_view($view_id, $display_id, array($order->order_id)); + } + else { + $pane_form['redeemed_coupons']['#markup'] = t('Your coupon code is not valid.'); + } + } + } + } + return $pane_form; } @@ -60,6 +102,33 @@ function commerce_coupon_pane_checkout_form_submit($form, &$form_state, $checkou if ($code = $form_state['values']['commerce_coupon']['coupon_code']) { $commerce_coupon = commerce_coupon_load_by_code($code); commerce_coupon_redeem_coupon($commerce_coupon, $order); + $form_state['rebuild'] = TRUE; + } +} + +/** + * Callback for the ajax button coupon_add. + */ +function commerce_coupon_add_coupon_callback($form, &$form_state) { + return $form['commerce_coupon']['redeemed_coupons']; +} + +/** + * Implements hook_ajax_render_alter(). + * + * This is to get the summary of the cart to rerender on the add coupon button. + */ +function commerce_coupon_ajax_render_alter(&$commands) { + $alter_coupon_commands = &drupal_static('alter_coupon_commands'); + $coupon_current_order = &drupal_static('coupon_current_order'); + + if ($alter_coupon_commands) { + $contents = array(); + $contents['cart_contents_view'] = array( + '#markup' => commerce_embed_view('commerce_cart_summary', 'default', array($coupon_current_order)), + ); + $contents = drupal_render($contents); + $commands[] = ajax_command_replace('.view-commerce-cart-summary', $contents); } } -- 1.7.4.4