Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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