Home Forums WoodMart support forum Cart Item Count

Cart Item Count

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #394287

    [email protected]
    Participant

    Hi guys,

    You provided me with the following code to hide the cart item count if the cart was empty.

    function woodmart_cart_count() {
    if ( ! is_object( WC() ) || ! property_exists( WC(), ‘cart’ ) || ! is_object( WC()->cart ) || !
    method_exists( WC()->cart, ‘get_cart_contents_count’ ) || WC()->cart->cart_contents_count == 0 ) {
    return;
    }

    $count = WC()->cart->get_cart_contents_count();
    ?>
    <span class=”wd-cart-number wd-tools-count<?php echo woodmart_get_old_classes( ‘ woodmart-cart-number’ ); ?>”><?php echo esc_html( $count ); ?> <span><?php echo esc_html( _n( ‘item’, ‘items’, $count, ‘woodmart’ ) ); ?></span></span>
    <?php
    }

    The only issue is that after adding an item to the cart you have to refresh the page for the item count to appear, please can you help me fix this?

    Thanks in advance.

    #394446

    Hello,

    I have submitted the case to our developers we will be able to get in touch with you on Monday.

    Best Regards

    #394820

    Hello,

    Please replace the code with this one:

    if ( ! function_exists( 'woodmart_cart_count') ) {
    	function woodmart_cart_count() {
    		if ( ! is_object( WC() ) || ! property_exists( WC(), 'cart' ) || ! is_object( WC()->cart ) || !
    			method_exists( WC()->cart, 'get_cart_contents_count' ) ) {
    			return;
    		}
    
    		$count   = WC()->cart->get_cart_contents_count();
    		$classes = 0 == $count ? ' wd-hide' : '';
    		?>
    		<span class="wd-cart-number wd-tools-count<?php echo woodmart_get_old_classes( 'woodmart-cart-number' ) . $classes; ?>">
    			<?php if ( 0 != $count ) : ?>
    				<?php echo esc_html( $count ); ?> <span><?php echo esc_html( _n( 'item', 'items', $count, 'woodmart' ) ); ?></span>
    			<?php endif; ?>
    		</span>
    		<?php
    	}
    }

    If you have any questions please feel free to contact us.

    Best Regards

    #394831

    [email protected]
    Participant

    That works perfectly, thanks a lot!

    #394914

    You are welcome! We are here to help.

    Wish you a wonderful day!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Cart Item Count’ is closed to new replies.