Skip to content

Instantly share code, notes, and snippets.

@DanielSantoro
Last active December 22, 2017 17:12
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DanielSantoro/1d0dc206e242239624eb71b2636ab148 to your computer and use it in GitHub Desktop.
Save DanielSantoro/1d0dc206e242239624eb71b2636ab148 to your computer and use it in GitHub Desktop.
Show Number of items in Cart and Subtotal (insert into template)
// Use in conjunction with https://gist.github.com/DanielSantoro/948c5000850c4695f30db8542f8bc966
<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
@vlcjohn
Copy link

vlcjohn commented Jul 24, 2017

This snippet works well when the user is logged in, but it doesn't seem to work when the user is not logged in and is a guests. It works while on the cart page itself, but all other pages show 0 quantity. Most likely a caching issue but am looking for a work around? Should I use jQuery and inject the number after page load?

@DanielSantoro
Copy link
Author

Hi there! You need to add Ajax to your custom cart display, which can be done with the snippet here: https://gist.github.com/DanielSantoro/948c5000850c4695f30db8542f8bc966

Basically, it won't update as needed until that Ajax code is paired with it, but after that it updates using the same values as a mini cart in Storefront or something like that. Hope this helps. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment