Skip Navigation

[Resolved] Cart Count when empty needs to display “0”

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here was that the user was using the default woocommerce cart count with views, however he noticed that when the cart was empty it didn't display anything. He wanted it to show 0 when the cart was empty.

Solution:

I wrote my own cart count function and should be able to provide you with a bit more flexibility with this.

Add this to your functions.php file and let me know if it helps.

// Add Shortcode
function cart_count() {
    if ( class_exists( 'WooCommerce' ) ) {
 global $woocommerce;
 
    ob_start();
        $total = $woocommerce->cart->cart_contents_count;
         
    return $total;

add_shortcode( 'cart_count', 'cart_count' );

This will just allow you to use the [cart_count] so you can remove all the conditionals from your layouts and just using this shortcode.

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 14 replies, has 2 voices.

Last updated by cristianR-3 6 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#603784

Tell us what you are trying to do? When no items are in the shopping cart in WooCommerce, I need it to show the cart-count as 0

Is there any documentation that you are following? Conditional Outputs

Is there a similar example that we can see? Yes, if you visit sunbeltimports.com you can see how it should work.

What is the link to your site? hidden link

#603918

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Cristian,

Thank you for contacting our support forum.

The 0 should show up there by default but it seems that there might be some custom css or plugin that is interfering with this.

What I recommend doing is to temporarily disable all the non-toolset plugins and checking again or removing any custom css.

Please let me know if this helps.
Thanks,
Shane

#603922

Shane, I don't have a lot of plugins with the exception of Toolset and WooCommerce. As far as custom CSS, wouldn't I be able to trouble shoot that with the browsers inspector? I really don't think the issue is with CSS because when I add an item, the number updates correctly, if there was a selector that was disabling the visibility, no number would show.

This is a PHP issue. When the cart has no value, nothing is displayed, when it has a value then it updates.

#603926

Ok I disabled everything with the exception of Toolset and WooCommerce, the issue persists.

#604387

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Christian,

I noticed that it remains empty as well. Does the problem persist on the shop page before toolset was used to customize it ?

Also if you switch the theme does it still remain ?

This is just to ensure that we are approaching the correct place to debug so we will know what exactly to do.

Looking forward to hearing from your soon.

Thanks,
Shane

#604441

With out Toolset customization, I would have to write my own function to implement it. I am using Views [wpv-woo-cart-count] Short code. Now I do know how to directly implement it using a custom function, but the sample code that I found was actually suing a if else statement to implement the 0 value inside the span tag. This leads me to believe that a similar implementation might be necessary using Views. I did attempt to it with a conditional output.

It could be my theme, but changing the theme is not trivial since I would have to use a Toolset friendly theme. Now the theme that I am using is a generic starter bootstrap/Genesis theme. I find it kind of hard to believe that it would be the issue, since I am very familiar with the CSS/JS involved in it's implementation.

I guess, my question to you or to your team would be, how can I force the short code to place "0" when no value is given? I attempted to do it with Conditional Output, but it does not seem to want to recognize the wpv-woo-cart-count short code. I think I am missing a step.

#604767

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Cristian,

I must apologize for the lengthy delays on this.

Would you mind showing me the attempt that was made to do this ?

I'll enable the private fields so that you can provide me with the credentials.

Looking forward to hearing from you soon.

Thanks,
Shane

#604897

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Cristian,

This is strange as the conditional shortcode is not working at all.

Would it be possible to get a duplicator package of the website ? This way I can perform some debugging steps and see what exactly is wrong here.

Thanks,
Shane

#604915

Sure, how do I send it to you?

#605192

Please use the link below to download the Duplicator installer and archive.

hidden link

#605243

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Cristian,

Thanks,

I'll let you know as soon as I find anything.

Thanks,
Shane

#605472

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Cristian,

Just an update on this. It seems that the issue is coming from Layouts specifically. Where the conditional shortcode doesn't work as intended.

I'm still investigation on this matter and see if I can come up with an alternative for you that will work just as well.

Thanks,
Shane

#605488

Thanks for your effort Shane. I see that under Toolset Settings/Front-end-content/Functions inside conditional evaluations the wpv-woo-cart-count function is already registered. But where is the original function declared? Maybe modifying the function would do the trick.

#605539

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Cristian,

I wrote my own cart count function and should be able to provide you with a bit more flexibility with this.

Add this to your functions.php file and let me know if it helps.


// Add Shortcode
function cart_count() {
	if ( class_exists( 'WooCommerce' ) ) {
 global $woocommerce;

	ob_start();
		$total = $woocommerce->cart->cart_contents_count;
		
	return $total;
}
}
add_shortcode( 'cart_count', 'cart_count' );

This will just allow you to use the [cart_count] so you can remove all the conditionals from your layouts and just using this shortcode.

Please let me know if this helps.
Thanks,
Shane

#605547

Shane, you came through. I appreciate your diligence in this matter. It is working as expected. I would think that it might be a good idea to make the developers aware of this issue. It really should be working with out conditional statements or simply suing the Views short code. But in any case it's working and thankful for your help.