Skip to content

Instantly share code, notes, and snippets.

@michaelbourne
Last active February 25, 2019 22:58
Show Gist options
  • Save michaelbourne/71f1f8e102a5b1d6e2c235a77dfdc8a9 to your computer and use it in GitHub Desktop.
Save michaelbourne/71f1f8e102a5b1d6e2c235a77dfdc8a9 to your computer and use it in GitHub Desktop.
Add a simple cart count to the Pro header builder cart elements
<?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();
$fragments['.cartdropdown .x-anchor-text-primary'] = '<span class="x-anchor-text-primary">' . $count . '</span>';
return $fragments;
}
.cartdropdown .x-anchor-text {
position: absolute;
top: -5px;
right: -5px;
background: #9ccf80;
border-radius: 1em;
width: 20px;
height: 20px;
text-align: center;
color: #fff;
line-height: 1.2;
z-index: 10;
opacity: 0;
pointer-events: none;
}
body.cart-has-items .cartdropdown .x-anchor-text {
opacity: 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment