Last active
March 2, 2024 12:40
-
-
Save Qubadi/85b94323019daad4ed53d755f21bc323 to your computer and use it in GitHub Desktop.
Seamlessly managing cart visibility in JetBlocks ( Woo Shopping Cart Widget ): hiding zero counts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this code to your site by creating a new PHP snippet in your snippet plugin. | |
Simply paste the code there and save it | |
____________________________________________________________________ | |
function custom_cart_visibility_script() { | |
?> | |
<style> | |
/* Initially hide the cart count to prevent it from showing 0 before JS checks */ | |
.jet-blocks-cart__count { | |
visibility: hidden; | |
} | |
</style> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
// Function to check the cart count and toggle visibility of the count wrapper | |
function toggleCartCountVisibility() { | |
var cartCount = parseInt($('.jet-blocks-cart__count-val').text()); | |
if (cartCount > 0) { | |
$('.jet-blocks-cart__count').css('visibility', 'visible'); // Show the count wrapper if count is more than 0 | |
} else { | |
$('.jet-blocks-cart__count').css('visibility', 'hidden'); // Keep the count wrapper hidden if count is 0 | |
} | |
} | |
// Initial check | |
toggleCartCountVisibility(); | |
// Listen for changes to the cart, using WooCommerce's fragments (this part may need adjustment based on your setup) | |
$(document.body).on('wc_fragments_loaded wc_fragments_refreshed', function() { | |
toggleCartCountVisibility(); | |
}); | |
// Alternative method if the cart is updated via AJAX in a custom way | |
// This requires custom triggers in your AJAX cart update code | |
$(document).on('cart_updated', function() { | |
toggleCartCountVisibility(); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action('wp_footer', 'custom_cart_visibility_script'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment