Tags/topics: 
2
Answers
Vote up!
0
Vote down!

Shopping cart in sidebar

Is there a module/view that adds a shopping cart block to the sidebar that updates when the product(s) on the current page are added to the cart? Ideally it should contain a form to let the user update their cart quantities as well,

Many thanks

Andy @ BlueFusion
on April 8, 2014

2 Answers

Vote up!
0
Vote down!

Here is some code that adds ajax capabilities to the cart/"add to cart"-button

<?php
function your_module_form_alter(&$form, &$form_state, $form_id) {
 
// Add ajax property to Add to Cart button
 
if (strstr($form_id, 'commerce_cart_add_to_cart_form')) {
   
$form['submit'] = array(
     
'#value' => t('add'),
     
'#type' => 'submit',
     
'#ajax' => array(
       
'callback' => 'your_module_form',
       
'wrapper' => 'block-commerce-cart-cart',
       
'effect' => 'fade',
       
'progress' => array('type' => 'throbber', 'message' => ''),
      ),
    );
   
$form['#submit'][] = 'your_module_form_rebuild_submit';
  }
}
function
your_module_form_rebuild_submit($form, &$form_state){
 
$form_state['rebuild'] = TRUE;
}

function
your_module_form($form, &$form_state){
  global
$user;
 
$order_id = commerce_cart_order_id($user->uid);
 
$cart commerce_embed_view('commerce_cart_block', 'default', array($order_id));
 
$commands = array();

 
$commands[] = ajax_command_replace('#block-commerce-cart-cart .view-commerce-cart-block', $cart);
 
$commands[] = ajax_command_replace('#block-commerce-cart-cart .cart-empty-block', $cart);
 
$commands[] = ajax_command_after('.'.$form['#attributes']['class'][1],theme('status_messages'));
  return array(
'#type' => 'ajax', '#commands' => $commands);
}
?>

Just add this code some where in a custom module and your cart should be actualised when an item is added.

Answer by: Johannes Kees
Posted: Apr 14, 2014