[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, 10 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.
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.
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.
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.
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.