Skip to content

Instantly share code, notes, and snippets.

@jamiemitchell
Last active December 22, 2021 18:44
Show Gist options
  • Save jamiemitchell/e1970b08fc96e561da99669e3ac9f5b5 to your computer and use it in GitHub Desktop.
Save jamiemitchell/e1970b08fc96e561da99669e3ac9f5b5 to your computer and use it in GitHub Desktop.
Add a Woocommerce cart icon with count in Genesis
<?php //* Do not include this opening php tag
//* Add Cart icon and count to header if WC is active
add_action( 'genesis_header', 'jmd_wc_cart_count' );
function jmd_wc_cart_count() {
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 ) echo '(' . $count . ')'; ?></a><?php
}
}
//* Ensure cart contents update when products are added to the cart via AJAX
add_filter( 'woocommerce_add_to_cart_fragments', 'jmd_header_add_to_cart_fragment' );
function jmd_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 ) echo '(' . $count . ')'; ?></a><?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
/* Cart icon
--------------------------------------------- */
.cart-contents {
background-color: #f5f5f5;
color: #333;
font-size: 16px;
padding: 16px 18px 17px;
position: absolute;
right: 60px;
top: 0;
z-index: 9999;
}
.cart-contents:before{
font-family: WooCommerce;
color: #333;
content: "\e01d";
font-size: 16px;
margin-top: 10px;
font-style: normal;
font-weight: 400;
padding-right: 5px;
vertical-align: bottom;
}
.cart-contents:hover {
background-color: #333;
color: #fff;
text-decoration: none;
}
.cart-contents:hover:before {
color: #fff;
}
@benjino
Copy link

benjino commented Dec 5, 2016

Hey this is awesome and I got it working. Any chance of having it include the total price?

Also, while I was able to get it into genesis_before_header where I have a custom menu showing up, I would love to be able to have it become one of the list items of the custom menu. Gallery-Pro has an Above Header widget area where I've placed a custom menu, that's where I'd love the cart count to display.

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