ARTICLES

JavaScript To Toggle WooCommerce Mini Cart In Bricks Builder

Below JavaScript function found in themes/bricks/assets/js/woocommerce.min.js

function bricksWooMiniModalsToggle(e) {
  e.preventDefault();
  var t = e.currentTarget.getAttribute('data-toggle-target');
  if (t) {
    document.querySelectorAll('.bricks-woo-toggle').forEach((function (e) {
      var r = e.getAttribute('data-toggle-target');
      if (r !== t) {
        var o = e.querySelector(r);
        if (null !== o && o.classList.contains('active')) {
          o.classList.remove('active');
          var i = e.closest('.brxe-woocommerce-mini-cart');
          i && i.classList.remove('show-cart-details')
        }
      }
    }));
    var r = document.querySelector(t);
    if (null !== r) {
      r.classList.toggle('active');
      var o = r.closest('.brxe-woocommerce-mini-cart');
      o && o.classList.toggle('show-cart-details')
    }
  }
}

......

function bricksWooMiniModals() {
  document.querySelectorAll('.bricks-woo-toggle').forEach((function (e) {
    e.addEventListener('click', bricksWooMiniModalsToggle)
  }))
}

......

document.addEventListener('DOMContentLoaded', (function (e) {
  bricksWooProductsFilter(),
  bricksWooMiniModals(),
  bricksWooQuantityTriggers(),
  bricksWooMiniCartHideDetailsClickOutside(),
  jQuery(document.body).on('updated_cart_totals', (function () 
    bricksWooQuantityTriggers(),
    bricksLazyLoad()
  ))
}));

From the script above, understand that Bricks will add click handler on DOMContentLoaded through bricksWooMiniModals() function.

The actual handler function is bricksWooMiniModalsToggle(e).

To reuse it, just manipulate the click event on .bricks-woo-toggle element will do. Not necessary to add or remove CSS classes like what already nicely wrote by Bricks team. Will be panic and busy if those CSS classes changed in future version.

// Simply trigger a click will execute bricksWooMiniModalsToggle function

document.querySelector('.bricks-woo-toggle').dispatchEvent( new Event('click') )

Wrote an article on how to create a customizable AJAX add to cart button in bricks on brickslabs few months ago. Refer to the tutorial JavaScript part, I can enhance and open the mini cart after add to cart event like this:

(($)=>{
	$(document).ready(()=>{
	
		// When Added To Cart
		$('body').on('added_to_cart', (e, fragments, cart_hash, button)=>{
			
			// Do nothing if the cart details already opened
			if ( document.querySelector('.cart-detail').classList.contains('active') ) return false
			
			// Trigger the click on mini cart toggle button
			document.querySelector('.bricks-woo-toggle').dispatchEvent( new Event('click') )
		});
	})
})(jQuery)
  • About Author

    I like to solve problems by coding. I like websites, web applications and everything viewable from browser. Knowledge sharing can grows my confidence. A simple "thank you" will make my day :)
    More about me

Subscribe For Notification

Get notification whenever new article published.

Discussion

COMMENT

Be the first to drop a comment.