简体   繁体   中英

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 ?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM