Advertisement
artiswilliams

pie_cart_ajax.module

Nov 29th, 2012
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  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.   //dpm($form_id);
  14.   $alter = strpos($form_id, 'views_form_commerce_cart_block_') === 0;
  15.   if ($alter) {
  16.     _pie_cart_ajax_form_views_form_alter($form, $form_state, $form_id);
  17.   }
  18. }
  19.  
  20. function _pie_cart_ajax_form_views_form_alter(&$form, $form_state, $form_id) {
  21.   //dpm($form);
  22.   unset($form['#action']);
  23.   unset($form['#submit']);
  24.  
  25.   $ajax = array (
  26.     'callback' => '_pie_cart_ajax_form_views_form_commerce_cart_block_callback',
  27.     'progress' => array(),
  28.     'effect' => 'fade',
  29.   );
  30.  
  31.   $form['actions']['submit']['#ajax'] = $ajax;
  32.   $form['actions']['submit']['#submit'][] = 'pie_cart_ajax_update_cart_submit';
  33.  
  34.  
  35.   if (!empty($form['edit_delete'])) {
  36.     foreach (element_children($form['edit_delete']) as $k) {
  37.       $form['edit_delete'][$k]['#value'] = t('X');
  38.       $form['edit_delete'][$k]['#ajax'] = $ajax;
  39.       $form['edit_delete'][$k]['#submit'][] = 'pie_cart_ajax_update_cart_submit';
  40.     }
  41.   }
  42.  
  43.   //$form['#ajax'] = $ajax;
  44.   //$form['#submit'][] = '_pie_cart_ajax_form_views_form_commerce_cart_block_submit';
  45.   dpm($form);
  46.  
  47.   return;
  48. }
  49.  
  50. function pie_cart_ajax_update_cart_submit($form, &$form_state) {
  51.   //dpm($form_state);
  52.   //unset($form_state['values']);
  53.   $form_state['rebuild'] = TRUE;
  54. }
  55.  
  56. function _pie_cart_ajax_form_views_form_commerce_cart_block_callback($form, $form_state) {
  57.   //dpm($form_state);
  58.   $cart = pie_cart_ajax_cart_reload(); //Retrieve the new cart view
  59.   $commands = array();
  60.  
  61.  
  62.   $commands[] = ajax_command_replace("#block-commerce-cart-cart .cart-empty-block", "<div class='cart-contents'></div>");
  63.   $commands[] = ajax_command_html('#block-commerce-cart-cart .cart-contents', $cart);
  64.   return array('#type' => 'ajax', '#commands' => $commands);
  65. }
  66.  
  67. function pie_cart_ajax_cart_reload() {
  68.   global $user;
  69.   $view_name = 'commerce_cart_block'; // The name of the view we are going to load
  70.   $args = array(commerce_cart_order_id($user->uid));  // Array of arguments we set for the view. Only one argument in our example. your actual view may require additional arguments which you may need to set
  71.  
  72.   commerce_cart_commerce_order_load($args);
  73.  
  74.   $display_id = 'default'; // The display id of for the view.
  75.  
  76.   // Call the views_embed_view function to returned themed view output
  77.  
  78.   $view = views_get_view($view_name, TRUE);
  79.   if (!$view || !$view->access($display_id)) {
  80.     return;
  81.   }
  82.  
  83.   $view->set_display($display_id);
  84.   $view->set_arguments($args);
  85.   $cart = $view->preview();
  86.  
  87.   return $cart;
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement