I have the module that changes the quantity of products inside the card via ajax but I need to change it also outside, as on the picture, enter image description here

the code that changes the quantity after reload of the page are as follows

$variables = array(
        'order' => $order,
        'contents_view' => commerce_embed_view('commerce_cart_block', 'defaults', array($order->order_id), $_GET['q']),
      $count = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());

      $quantity = 0;

      foreach ($wrapper->commerce_line_items as $line_item) {
        if (!$line_item instanceof EntityMetadataWrapper) {
          $line_item = entity_metadata_wrapper('commerce_line_item', $line_item);
        $types = array('product');

        if (empty($types) || in_array($line_item->type->value(), $types)) {
          $quantity = $quantity + $line_item->quantity->value();


      $prod_count = t($quantity);  

      if ($prod_count > 0){

        $icon = '<div class="cart-icon"></div><span class="cart_popup_count">'. $count . '</span>';
        $content = '<div id="cart-popup" style="display:none;">' . theme('commerce_cart_block', $variables) . '<div class="popup-arrow"></div></div>';
        $content = '<div class="wrapper">' . $icon . $content . '</div>';  
      }elseif (variable_get('commerce_popup_cart_show_empty_cart', 0) == 1){
        $content = commerce_popup_cart_block_view_get_empty_cart($variables);

The problem is that I don't know how to changes this numbers simultaneously inside and outside the card.

This is the code that changes quantity inside the card

  function _commerce_popup_cart_add_to_cart_ajax_callback($form, $form_state) {
  commerce_cart_add_to_cart_form_submit($form, $form_state); //Call the add to cart form to actually add the product
  $res = _commerce_popup_cart_ajax_cart_reload(); //Retrieve the new cart view

  $commands = array();
  $commands[] = ajax_command_replace("#block-commerce-popup-cart-commerce-popup-cart .cart-empty-block", "<div class='cart-contents'></div>");
  $commands[] = ajax_command_html('#block-commerce-popup-cart-commerce-popup-cart .cart-contents', $res);
  return array('#type' => 'ajax', '#commands' => $commands); 

function _commerce_popup_cart_ajax_cart_reload() {
  global $user;
  $view_name = 'commerce_cart_block'; // The name of the view we are going to load commerce_cart_block
  $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   
  $displayId = 'default'; // The display id of for the view.
  // Call the views_embed_view function to returned themed view output
  $res = views_embed_view($view_name, $displayId, $args);
  return $res;
  • i did not read all your code ,, but i want to understand .. you know how to update card as php .. but you do not know how to do it as js ?? right ? Commented Aug 4, 2016 at 12:36
  • yes that's right
    – user63301
    Commented Aug 5, 2016 at 6:47
  • I wrote such a code but it didn't help me $('.cart-selector').on('change', function(e)) { $('.your-another-place').text($(this).text()); }
    – user63301
    Commented Aug 6, 2016 at 8:30
  • so i thing you have to create new simple module which has hook_menu to create new link like mysite/card/update/total .. then call this link as Ajax in your js Commented Aug 8, 2016 at 7:22

1 Answer 1

  • Create new simple module like my_updater_card
  • add hook_menu
  • add php function to update the card
  • call this function from ajax

     * Implements hook_menu().
     * see hook_menu()
     * see menu_example
    function my_updater_card_menu()
        $items['my_updater/card/%'] = array(
            'page callback'         => 'my_updater',
            'page arguments'        => array(2),
            'access arguments'      => array('put your permission here'),
        return $items;
    function my_updater($newTotal){
        //check if this request come from ajax => if not => ignore it
        //here put your php code to update the card
        //then return results true/false to ajax caller


               url: 'my_updater/card/[new total here]',
               type: "GET",
               success: function (data) {
                 //do total refresh here 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.