Dr.MTR
Dr.MTR

Reputation: 207

Woocommerce Cart Count not count added products outside Cart page?

I working on custom site, and get some very weird issue. I use this code to show added cart product count:

<li><a class="<?php echo (is_page('favorites')) ? 'active' : '' ?>" 
href="/cart">CART(<span id="cart-count"><?php echo count($woocommerce->cart-
>cart_contents); ?></span>)</a></li>

and also tryed to replace

<?php echo count($woocommerce->cart->cart_contents); ?>

with

<?php echo WC()->cart->get_cart_contents_count(); ?>

but without success. The weird thing is that on Staging site is working just fine. I moved exact files on main site, but this problem occured. I dont know what really is issue. Cart count show only when click on /cart page and nowhere else, but on staging site is working anywhere, is showing number on products in cart, not depending where user is navigating. Any tip ?

Upvotes: 1

Views: 311

Answers (1)

developerme
developerme

Reputation: 1955

You can Try This code

<?php 
    $count = WC()->cart->cart_contents_count;
?>
<li><a class="<?php echo (is_page('favorites')) ? 'active' : '' ?>" 
href="<?php echo WC()->cart->get_cart_url(); ?>">CART
<?php if ( $count > 0 ) { ?>
<span id="cart-count"><?php echo esc_html( $count ); ?></span>)
<?php } ?>
</a></li>

Upvotes: 2

Related Questions