Changing Header cart-number to show when count > 0
-
Hello,
I added this code to my functions.php of the Child Theme.
I added it to this hook woocommerce_add_to_cart
but when I add products to the cart it loads forever till I refresh the page.
My Code:
// Changing Header cart-number to show when count > 0
function woodmart_cart_count() {
$count = WC()->cart->get_cart_contents_count();
if($count) { ?>
<span class="woodmart-cart-number"><?php echo esc_html($count); ?> <span><?php echo esc_html( _n( 'item', 'items', $count, 'woodmart' ) ); ?></span></span>
<?php }
}
// add_action('woocommerce_add_to_cart', 'woodmart_cart_count');
// add_action('woocommerce_add_to_cart', 'woodmart_cart_count',10);
Kind Regards,
Harry
Hello,
Sorry, but it is not an official code and we don’t provide support for such additional customizations.
Kind Regards
Hello,
but I just added an if statement to the default woodmart_cart_count()
which works fine.
My problem was with the hook and if you know if I can use another one.
Thank you anyways
Yes, but when you add something to the cart, it calls this function with AJAX and the script expects some HTML to be returned. So by customizing this code you break other parts of the theme.