Skip to content

Instantly share code, notes, and snippets.

@michaelbourne
Last active August 24, 2018 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbourne/27b2a07ec3452c8f6ec79044d57f0219 to your computer and use it in GitHub Desktop.
Save michaelbourne/27b2a07ec3452c8f6ec79044d57f0219 to your computer and use it in GitHub Desktop.
Add the Woocommerce cart count and cart total to a cart element in the Pro header builder.
<?php
// Add cart count and value to Cart Element text in a Pro header
// =============================================================================
add_filter('woocommerce_add_to_cart_fragments', 'mb_cart_count_fragments', 10, 1);
function mb_cart_count_fragments($fragments) {
$count = WC()->cart->get_cart_contents_count();
$value = ($count == 0) ? '$0.00' : WC()->cart->get_cart_total();
$cart = ($count == 0) ? 'Cart' : 'Items: ' . $count;
$fragments['.cartdropdown .x-anchor-text-secondary'] = '<span class="x-anchor-text-secondary">' . $value . '</span>';
$fragments['.cartdropdown .x-anchor-text-primary'] = '<span class="x-anchor-text-primary">' . $cart . '</span>';
return $fragments;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment