Skip to content

Instantly share code, notes, and snippets.

@neilgee neilgee/cart-add-menu.php
Last active Sep 3, 2019

Embed
What would you like to do?
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 {
color: #fff;
background: transparent;
font-weight: bold;
border-radius: 50%;
padding: 1px 6px;
line-height: 1.5;
font-family: Arial, Helvetica, sans-serif;
vertical-align: top;
height: 17px;
width: 17px;
transform: translateY(-105%) translateX(25%);
font-size: 10px;
}
<?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
You can’t perform that action at this time.