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, you can use these sample codes. Replace the values appropriately in the code so that it can correctly focus on the cart drawer in your theme. (theme.liquid file)
1. Add the following code.

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

2. Or add the CSS below inside the` <head></head>` tags of the theme.liquid file.

{% style %}
#wa .wa__btn_popup,
#wa .wa__popup_chat_box {
  z-index: 30 !important;
}
{% endstyle %}

Was this article helpful to you? No Yes

How can we help?