Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active August 26, 2021 19:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neilgee/2de0785ae6679b6be3a8f2ebf452c245 to your computer and use it in GitHub Desktop.
Save neilgee/2de0785ae6679b6be3a8f2ebf452c245 to your computer and use it in GitHub Desktop.
WooCommerce Cart Icon
<?php // <~ don't add me in
add_filter( 'wp_nav_menu_top-menu_items', 'woo_cart_but_icon', 10, 2 ); // Change menu to suit - example uses 'top-menu'
/**
* Add WooCommerce Cart Menu Item Shortcode to particular menu
*/
function woo_cart_but_icon ( $items, $args ) {
$items .= '[woo_cart_but]'; // Adding the created Icon via the shortcode already created
return $items;
}
/* # WooCommerce Cart Icon CSS with FontAwesome 5
---------------------------------------------------------------------------------------------------- */
.cart-contents {
position: relative;
display: flex !important;
flex-flow: column nowrap;
justify-content: center;
}
.cart-contents:before {
font-family: 'Font Awesome 5 Free';
font-weight: 900;
content: "\f290" !important;
font-size: 30px;
color: #FF1493;
}
.cart-contents:hover {
text-decoration: none;
}
.cart-contents-count {
position: absolute;
top: 15px;
right: 1px;
transform: translateY(-105%) translateX(25%);
font-family: Arial, Helvetica, sans-serif;
font-weight: normal;
font-size: 12px;
line-height: 22px;
height: 22px;
width: 22px;
vertical-align: middle;
text-align: center;
color: #fff;
background: #000;
border-radius: 50%;
padding: 1px;
}
<?php // <~ don't add me in
add_filter( 'woocommerce_add_to_cart_fragments', 'woo_cart_but_count' );
/**
* Add AJAX Shortcode when cart contents update
*/
function woo_cart_but_count( $fragments ) {
ob_start();
$cart_count = WC()->cart->cart_contents_count;
$cart_url = wc_get_cart_url();
?>
<a class="cart-contents menu-item" href="<?php echo $cart_url; ?>" title="<?php _e( 'View your shopping cart' ); ?>">
<?php
if ( $cart_count > 0 ) {
?>
<span class="cart-contents-count"><?php echo $cart_count; ?></span>
<?php
}
?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
<?php // <~ don't add me in
add_shortcode ('woo_cart_but', 'woo_cart_but' );
/**
* Create Shortcode for WooCommerce Cart Menu Item
*/
function woo_cart_but() {
ob_start();
$cart_count = WC()->cart->cart_contents_count; // Set variable for cart item count
$cart_url = wc_get_cart_url(); // Set Cart URL
?>
<li><a class="menu-item cart-contents" href="<?php echo $cart_url; ?>" title="My Basket">
<?php
if ( $cart_count > 0 ) {
?>
<span class="cart-contents-count"><?php echo $cart_count; ?></span>
<?php
}
?>
</a></li>
<?php
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment