SHARE
TWEET

pie_cart_ajax.module [working]

artiswilliams Jan 18th, 2013 366 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * @file
  4.  * Drupal Commerce Cart Ajax module.
  5.  * Ajax features for Drupal Commerce Cart. Quick item removal and quantity changes.
  6.  */
  7.  
  8. /**
  9.  * Implements hook_form_FORM_ID_alter().
  10.  * Alter to cart form to add #ajax properties.
  11.  */
  12. function pie_cart_ajax_form_views_form_alter(&$form, $form_state, $form_id) {
  13.   $alter = strpos($form_id, 'views_form_commerce_cart_form_') === 0;
  14.   if ($alter) {
  15.     _pie_cart_ajax_form_views_form_alter($form, $form_state, $form_id);
  16.   }
  17. }
  18.  
  19. function _pie_cart_ajax_form_views_form_alter(&$form, $form_state, $form_id) {
  20.   unset($form['#action']);
  21.   $submit = $form['#submit'];
  22.   unset($form['#submit']);
  23.  
  24.   $ajax = array (
  25.     'callback' => 'pie_cart_ajax_update_cart_callback',
  26.     'progress' => array(),
  27.     'effect' => 'fade',
  28.   );
  29.  
  30.   // Update Cart Button
  31.   $form['actions']['submit']['#ajax'] = $ajax;
  32.   $form['actions']['submit']['#submit'][] = 'pie_cart_ajax_update_cart_submit';
  33.  
  34.   // Checkout Button
  35.   $form['actions']['checkout']['#type'] = 'button';
  36.   $form['actions']['checkout']['#ajax'] = array(
  37.     'callback' => 'pie_cart_ajax_checkout_redirect_callback',
  38.     'progress' => array(),
  39.   );
  40.  
  41.   if ($form_state['submitted']) {
  42.     if (!empty($form['edit_quantity'])) {
  43.       foreach (element_children($form['edit_quantity']) as $j) {
  44.         $line_item_id = $form['edit_quantity'][$j]['#line_item_id'];
  45.         $form['edit_quantity'][$j]['#default_value'] = round($form_state['line_items'][$line_item_id]->quantity);
  46.       }
  47.     }
  48.   }
  49.   // Delete Buttons
  50.   if (!empty($form['edit_delete'])) {
  51.     foreach (element_children($form['edit_delete']) as $k) {
  52.       $form['edit_delete'][$k]['#value'] = t('X');
  53.       $form['edit_delete'][$k]['#ajax'] = $ajax;
  54.       $form['edit_delete'][$k]['#submit'][] = 'pie_cart_ajax_update_cart_submit';
  55.     }
  56.   }
  57.   dpm($form);
  58.   return;
  59. }
  60.  
  61. function pie_cart_ajax_checkout_redirect_callback() {
  62.   ctools_include('ajax');
  63.   ctools_add_js('ajax-responder');
  64.   $commands[] = ctools_ajax_command_redirect('checkout');
  65.   print ajax_render($commands);
  66.   exit;
  67. }
  68.  
  69. function pie_cart_ajax_update_cart_submit($form, &$form_state) {
  70.   $form_state['storage'] = array(
  71.     'trigger' => $form_state['triggering_element'],
  72.     'start_quantity' => $form_state['complete form']['edit_quantity'],
  73.     'end_quantity' => $form_state['values']['edit_quantity'],
  74.     'view' => $form_state['build_info']['args']['0']->result,  // OPTIONAL: only needed for callback additional process
  75.   );
  76.   $form_state['rebuild'] = TRUE;
  77. }
  78.  
  79. function pie_cart_ajax_update_cart_callback($form, $form_state) {
  80.   global $user;
  81.   $cart = pie_cart_ajax_cart_reload(); //Retrieve the new cart view
  82.  
  83.   $errors = drupal_get_messages('status', TRUE); // Clear cart related messages
  84.  
  85.   $commands = array();
  86.  
  87.   //additional processing code not related to ajaxifying the cart goes here
  88.  
  89.   $commands[] = ajax_command_replace('.view-commerce-cart-form', $cart);
  90.   //$commands[] = ajax_command_append('.view-commerce-cart-form', $errors);
  91.   return array('#type' => 'ajax', '#commands' => $commands);
  92. }
  93.  
  94. function pie_cart_ajax_cart_reload() {
  95.   global $user;
  96.   $view_name = 'commerce_cart_form';
  97.   $cart_order = commerce_cart_order_load($user->uid);
  98.  
  99.   if (!$cart_order || empty($cart_order->commerce_line_items)) {
  100.     $cart = '<div class="view-commerce-cart-form">' . theme('commerce_cart_empty_block') . '</div>';
  101.   }
  102.   else {
  103.     $args = array($cart_order->order_number);
  104.     $display_id = 'default'; // The display id of for the view.
  105.     $view = views_get_view($view_name);
  106.  
  107.     if (!$view || !$view->access($display_id)) {
  108.       return;
  109.     }
  110.  
  111.     $view->set_display($display_id);
  112.     $view->set_arguments($args);
  113.     $cart = $view->preview();
  114.   }
  115.   return $cart;
  116. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top