Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * Drupal Commerce Cart Ajax module.
- * Ajax features for Drupal Commerce Cart. Quick item removal and quantity changes.
- */
- /**
- * Implements hook_form_FORM_ID_alter().
- * Alter to cart form to add #ajax properties.
- */
- function pie_cart_ajax_form_views_form_alter(&$form, $form_state, $form_id) {
- //dpm($form_id);
- $alter = strpos($form_id, 'views_form_commerce_cart_block_') === 0;
- if ($alter) {
- _pie_cart_ajax_form_views_form_alter($form, $form_state, $form_id);
- }
- }
- function _pie_cart_ajax_form_views_form_alter(&$form, $form_state, $form_id) {
- //dpm($form);
- unset($form['#action']);
- unset($form['#submit']);
- $ajax = array (
- 'callback' => '_pie_cart_ajax_form_views_form_commerce_cart_block_callback',
- 'progress' => array(),
- 'effect' => 'fade',
- );
- $form['actions']['submit']['#ajax'] = $ajax;
- $form['actions']['submit']['#submit'][] = 'pie_cart_ajax_update_cart_submit';
- if (!empty($form['edit_delete'])) {
- foreach (element_children($form['edit_delete']) as $k) {
- $form['edit_delete'][$k]['#value'] = t('X');
- $form['edit_delete'][$k]['#ajax'] = $ajax;
- $form['edit_delete'][$k]['#submit'][] = 'pie_cart_ajax_update_cart_submit';
- }
- }
- //$form['#ajax'] = $ajax;
- //$form['#submit'][] = '_pie_cart_ajax_form_views_form_commerce_cart_block_submit';
- dpm($form);
- return;
- }
- function pie_cart_ajax_update_cart_submit($form, &$form_state) {
- //dpm($form_state);
- //unset($form_state['values']);
- $form_state['rebuild'] = TRUE;
- }
- function _pie_cart_ajax_form_views_form_commerce_cart_block_callback($form, $form_state) {
- //dpm($form_state);
- $cart = pie_cart_ajax_cart_reload(); //Retrieve the new cart view
- $commands = array();
- $commands[] = ajax_command_replace("#block-commerce-cart-cart .cart-empty-block", "<div class='cart-contents'></div>");
- $commands[] = ajax_command_html('#block-commerce-cart-cart .cart-contents', $cart);
- return array('#type' => 'ajax', '#commands' => $commands);
- }
- function pie_cart_ajax_cart_reload() {
- global $user;
- $view_name = 'commerce_cart_block'; // The name of the view we are going to load
- $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
- commerce_cart_commerce_order_load($args);
- $display_id = 'default'; // The display id of for the view.
- // Call the views_embed_view function to returned themed view output
- $view = views_get_view($view_name, TRUE);
- if (!$view || !$view->access($display_id)) {
- return;
- }
- $view->set_display($display_id);
- $view->set_arguments($args);
- $cart = $view->preview();
- return $cart;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement