Skip to content

Instantly share code, notes, and snippets.

@geektutor
Forked from jamiemitchell/functions.php
Created April 16, 2018 13:58
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 geektutor/dcb60d42f1f9b27b96d4dc93766c3d5d to your computer and use it in GitHub Desktop.
Save geektutor/dcb60d42f1f9b27b96d4dc93766c3d5d 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment