Site logo

Archived topic

Cart Icon doesnt show 0 number when no items in the cart

2 replies · Started by johnaps on January 31, 2021

Viewing posts 1–3 of 3

Hello GP Team!

I have implemented the following code in my functions.php for my custom homepage show i can show the cart.


add_shortcode ('woo_cart_but', 'woo_cart_but' );
/**
 * Create Shortcode for WooCommerce Cart Menu Item
 */
function woo_cart_but() {
	ob_start();
 
        $cart_count = WC()->cart->cart_contents_count; // Set variable for cart item count
        $cart_url = wc_get_cart_url();  // Set Cart URL
  
        ?>
        <a class="hp-cart-contents" href="<?php echo $cart_url; ?>"Το καλάθι μου"><img src="https://staging-sofianosorthopedikagr.kinsta.cloud/wp-content/uploads/2021/01/cart-icon-gray.png">
	    <?php
        if ( $cart_count > 0 ) {
       ?>
            <span class="hp-cart-contents-count"><?php echo $cart_count; ?></span>
        <?php
        }
        ?>
        </a>
        <?php
	        
    return ob_get_clean();
 
}
add_filter( 'woocommerce_add_to_cart_fragments', 'woo_cart_but_count' );
/**
 * Add AJAX Shortcode when cart contents update
 */
function woo_cart_but_count( $fragments ) {
 
    ob_start();
    
    $cart_count = WC()->cart->cart_contents_count;
    $cart_url = wc_get_cart_url();
    
    ?>
    <a class="hp-cart-contents" href="<?php echo $cart_url; ?>" title="<?php _e( 'Δες το καλάθι σου' ); ?>">
	<?php
    if ( $cart_count > 0 ) {
        ?>
        <span class="hp-cart-contents-count"><?php echo $cart_count; ?></span>
        <?php            
    }
        ?></a>
    <?php
 
    $fragments['a.hp-cart-contents'] = ob_get_clean();
     
    return $fragments;
}

It works beautifully but there is no number when i have 0 items in the cart...

I have encountered this problem before in my cart in other pages and you were able to help me...

Can you help me out?

https://staging-sofianosorthopedikagr.kinsta.cloud/

I found out that this css

span.hp-cart-contents-count:before {
    content: "0";
}

shows the zero in the right position!

But if there is already a number in the cart (1, 2, 3...) the 0 goes in front of them!

Could i somehow make it show only when 0 items in the cart?

Hi there,

Unfortunately we cannot help with custom solutions here.

You will likely need to add another condition for:

if ( $cart_count > 0 ) {
       ?>
            <span class="hp-cart-contents-count"><?php echo $cart_count; ?></span>
        <?php

Not sure where the hp-cart-content is supposed to do? Is that from a plugin?

If so I would recommend checking with their support team.

Another option is to open a topic in a general WordPress forum like this:
https://wordpress.stackexchange.com/

Thanks for your understanding :)

This archived topic is closed to new replies.