Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Last active October 30, 2018 14:40
Show Gist options
  • Save nfsarmento/1c0118d86d0b0ffcdd7cfa146e6823fb to your computer and use it in GitHub Desktop.
Save nfsarmento/1c0118d86d0b0ffcdd7cfa146e6823fb to your computer and use it in GitHub Desktop.
Add WooCommerce Cart Icon With Cart Count To Your Theme’s Header
/**
* Ensure cart contents update when products are added to the cart via AJAX
*
* https://www.nuno-sarmento.com
*/
function my_header_add_to_cart_fragment( $fragments ) {
ob_start();
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a><?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
<?php if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a>
<?php } ?
.cart-contents:before {
font-family:WooCommerce;
content: "\e01d";
font-size:28px;
margin-top:10px;
font-style:normal;
font-weight:400;
padding-right:5px;
vertical-align: bottom;
}
.cart-contents:hover {
text-decoration: none;
}
.cart-contents-count {
color: #fff;
background-color: #2ecc71;
font-weight: bold;
border-radius: 10px;
padding: 1px 6px;
line-height: 1;
font-family: Arial, Helvetica, sans-serif;
vertical-align: top;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment