I only display AJAX shopping cart teaser in my blocks. When I click on add to cart, I see the confirmation message but the AJAX shopping cart teaser does not update the number of items currently in the cart, you need to refresh the page to see the current cart content.

If you have both AJAX shopping cart & AJAX shopping cart teaser displaying, Teaser does update.

I use commerce_kickstart 7.x-2.28, dc_ajax_add_cart 7.x-2.1, jquery_update 7.x-3.0-alpha2
Default jQuery version 1.8

Thanks in advance for this new feature!

Comments

mchamps created an issue. See original summary.

anybody’s picture

Thank you very much for this separate issue after discussion in: #2388483: Ajax Shopping Cart Teaser not updates with Ajax

The reason seems to be that the AJAX changes are not applied to the teaser wrapper div if the main block is missing. As an UNCLEAN QUICKFIX / workaround you can add the main block but hide it by CSS.

For a final fix we should find out why the AJAX manipulations do not work. Perhaps the main block manipulation should simply be removed if the block is not used. That might fix it and make things cleaner.

mchamps’s picture

Thanks for the quick dirty fix, didn't think of it!
I'll do that for now.

grougy’s picture

Hi,
I had the same issue, this module is the best one even if you only need the shopping cart teaser.
The solution to have the teaser updating is to add the js include in the block view of the teaser.
(the js that adds a custom ajax command)

In dc_ajax_add_cart.module,
function dc_ajax_add_cart_block_view,
case 'ajax_shopping_cart_teaser' :

...
      ));
      $block['content'] .= '</div>';

      drupal_add_css(drupal_get_path('module', 'dc_ajax_add_cart') . '/css/dc_ajax_add_cart.css');
      // Added to have the teaser working without the main block
      drupal_add_js(drupal_get_path('module', 'dc_ajax_add_cart') . '/js/dc_ajax_add_cart_html.js');

      break;
  }
  return $block;
...

We still have useless ajax commands trying to update main block but it is cleaner than hidding the main block with css.
What do you think about that ?

grougy’s picture

My proposal at #4 require a fix in the script dc_ajax_add_cart_html.js
We need to test if Drupal.ajax exists as with only Teaser block we may be on pages without ajax at all and it leads to a js error :

/**
 * @file
 * Creates a Ajax 'replace' command.
 *
 * This command is specifically for dc_ajax_add_cart module.
 * Unlike `ajax_command_replace()`, this command will not produce extra div
 * wrapper with 'display: block' style.
 */

(function ($) {
  // Testing if Drupal.ajax exists as we may be on a page without ajax at all (When using only teaser block)
  if (Drupal.ajax) { 
    Drupal.ajax.prototype.commands.dc_ajax_add_cart_html = function(ajax, response, status) {
      
      // Get information from the response. If it is not there, default to
      // our presets.
      var wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);

      var new_content = $(response.selector).html(response.data);

      // If removing content from the wrapper, detach behaviors first.
      var settings = response.settings || ajax.settings || Drupal.settings;
      Drupal.detachBehaviors(wrapper, settings);

      // Attach all JavaScript behaviors to the new content, if it was successfully
      // added to the page, this if statement allows #ajax['wrapper'] to be
      // optional.
      if (new_content.parents('html').length > 0) {
        // Apply any settings from the returned JSON if available.
        var settings = response.settings || ajax.settings || Drupal.settings;
        Drupal.attachBehaviors(new_content, settings);
      }
    };
  }
})(jQuery);

subhojit777’s picture

Status: Active » Fixed

I've used the solution as mentioned in #2388483-9: Ajax Shopping Cart Teaser not updates with Ajax, it is a lot cleaner. Even if you add the js multiple times, it is going to be included in the HTML only once. Drupal takes care of that. Also it makes sure that the two blocks are independent.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.