Auto-hide widget when the cart drawer opens

  1. Home
  2. Docs
  3. WhatsApp
  4. Set up
  5. Developer’s zone
  6. Auto-hide widget when the cart drawer opens

Auto-hide widget when the cart drawer opens

To automatically hide the floating WhatsApp widget when the cart drawer opens, use this sample code. Replace the values appropriately in the code so that it can correctly focus on the cart drawer in your theme. (theme.liquid file)

document.addEventListener("DOMContentLoaded", function() {
	// Replace '#CartDrawer' with the correct cart drawer selector from your theme, number 1 in the illustration
	const el = document.querySelector('#CartDrawer');
	const waDiv = document.getElementById('wa');
	
	if (el && waDiv) {
	    const observer = new MutationObserver(() => {
	        // Replace 'drawer--is-open' with the class added when the cart drawer is opened, number 2 in the illustration
	        if (el.classList.contains('drawer--is-open')) {
	            // Hide the WhatsApp floating widget when the cart drawer is open
	            window.sgWaGlobalFunctions.sgWaCloseFloatingWidget();
	
	        } else {
	            // Show the WhatsApp floating widget when the cart drawer is closed
	            window.sgWaGlobalFunctions.sgWaOpenFloatingWidget();
	        }
	    });
	    observer.observe(el, {
	        attributes: true,
	        attributeFilter: ['class']
	    });
	}
});
code to hide widget
Was this article helpful to you? No Yes

How can we help?