Commerce : reload commerce_cart_form view with ajax

I’m coupling Drupal commerce, DC add to cart ajax and commerce flyout modules.

This is my view configuration :

enter image description here

On my /cart page, I have the initial form (the view) to change the quantities, delete the items etc…

I also have a slider of product, which thanks to the "ajax add to cart" module, I can add several product to the cart.

The expected behavior :

  • Whenever I update the quantity or remove an item from the flyout, the form need to be updated (including the total)
  • Whenever I update the quantity or remove an item from the form, the flyout need to be reloaded (included the total)
  • Whenever I update the quantity or remove from the slider, both of the flyout and the form needs to be update.

In js, i’ve tried to triggered them individualy after the buttons have been clicked, by calling custom ajax code to rebuild the view and the flyout.

/**  * Reload all the page content view (form)  */ function rebuildViewPanier() {     //$("#commerce_cart_form-cart-ajax-wrapper").triggerHandler("RefreshView");     $.ajax({         url: "/ajax_update_cart_panier"     }).done(function (data) {         if (data[3] !== undefined) {             let selector = data[3]["selector"];             let dataReplace = data[3]["data"];             let block = $(selector);             block.html($(dataReplace));         }     }); }   /**  * Code from the flyout module  */ function rebuildBlockCart() {     let el = document.querySelector(".cart-flyout");     // Drupal.cartFlyout.createFlyout();     let settings = drupalSettings.cartFlyout;     settings.use_quantity_count = true;     var model = new Drupal.cartFlyout.CartBlockModel(settings);     Drupal.cartFlyout.models.push(model);     var view = new Drupal.cartFlyout.CartBlockView({         el: el,         model: model     });     var offcanvasView = new Drupal.cartFlyout.CartOffcanvasView({         el: Drupal.cartFlyout.offcanvas,         model: model     });     Drupal.cartFlyout.views.push(view);     Drupal.cartFlyout.views.push(offcanvasView);     Drupal.cartFlyout.fetchCarts(); } 

Actual behavior: for now, those interactions between those 3 parts of the page are like this

  • Slider –> flyout :: working (automaticaly)
  • Form –> slider :: working (automaticaly)
  • Slider –> form :: KO (when i’m reloading it manualy)

And this is the slider–>form where i’m stuck.

The form isn’t reloaded (so I made the ajax function to load it manualy).

This is how I rebuild it :

    $view_name = "commerce_cart_form";     $display_id = "default";     $view = [         '#type' => 'view',         '#name' => $view_name,         '#display_id' => $display_id,         '#arguments' => [$cart_id],         '#embed' => TRUE,     ];     $content = Drupal::service('renderer')->render($view);     $response = new AjaxResponse();     return $response->addCommand(new ReplaceCommand('#commerce_cart_form-cart-ajax-wrapper', $content)); 

The form is rebuild, but I’m losing any event on the ajax button to update the cart.

The inital HTML

<form data-drupal-selector="views-form-commerce-cart-form-default-26" action="/cart" method="post"       id="views-form-commerce-cart-form-default-26" accept-charset="UTF-8" data-once="form-updated"       data-drupal-form-fields="edit-dc-ajax-add-cart-views-edit-quantity-0,edit-remove-button-0,edit-dc-ajax-add-cart-views-edit-quantity-1,edit-remove-button-1,edit-dc-ajax-add-cart-views-edit-quantity-2,edit-remove-button-2,edit-dc-ajax-add-cart-views-edit-quantity-3,edit-remove-button-3,edit-dc-ajax-add-cart-views-edit-quantity-4,edit-remove-button-4,edit-coupon-redemption-code,edit-coupon-redemption-apply,edit-submit,edit-checkout"> 

The HTML after reloading :

<form data-drupal-selector="views-form-commerce-cart-form-default-26" action="/ajax_update_cart_panier" method="post" id="views-form-commerce-cart-form-default-26" accept-charset="UTF-8"> 

As you can see, I’m using all my data-drupal-form-fields. Is it by because of that, that the initial form isn’t reloading it manualy ?

The triggerHandler(‘RefreshView’) is called like 10 times when it’s called (about 15/20s to refresh the view, didn’t have that problem before updating the core version), that’s why I decided to use custom ajax too rebuild the view.

I’m using the latest version of the modules and i’m using drupal 9.3.7

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Commerce : reload commerce_cart_form view with ajax

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.