Append cart icon to end of main nav menu

I’m using a custom theme and want to append a cart icon to the main nav menu. This is my site.

www.allianchemical.com/staging

I used the theme_location to select for the primary navigation as it’s defined in my functions.php in my parent theme, however the primary menu must’ve have been used twice on the same page in my theme, because the cart icon show sup twice. How do I work around this.

// Add Font Awesome to site.
add_action( ‘wp_enqueue_scripts’, ‘dcwd_include_font_awesome_css’ );
function dcwd_include_font_awesome_css() {
// Enqueue Font Awesome from a CDN.
wp_enqueue_style( ‘font-awesome-cdn’, ‘https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css’ );
}

// Style the cart count number.
add_action( ‘wp_head’, ‘dcwd_cart_count_styles’ );
function dcwd_cart_count_styles() {
?>
<style>
#menu-main-nav.cart { position: relative; margin-top: auto; margin-bottom: auto; }
#menu-main-nav .count { background: #666; color: #fff; border-radius: 2em; height: 18px; line-height: 18px; position: absolute; right: 5px; text-align: center; top: 90%; transform: translateY(-100%) translateX(15%); width: 18px; }
</style>
<?php
}

add_filter( ‘wp_nav_menu_items’, ‘am_append_cart_icon’, 10, 2 );
function am_append_cart_icon( $items, $args ) {
if( $args->theme_location == ‘Primary Navigation’)
{
$cart_item_count = WC()->cart->get_cart_contents_count();
$cart_count_span = ”;
if ( $cart_item_count ) {
$cart_count_span = ‘<span class=”count”>’.$cart_item_count.'</span>’;
}
$cart_link = ‘<li class=”cart menu-item menu-item-type-post_type menu-item-object-page”><a href=”‘ . get_permalink( wc_get_page_id( ‘cart’ ) ) . ‘”><i class=”fa fa-shopping-bag”></i>’.$cart_count_span.'</a></li>’;

// Add the cart link to the end of the menu.
$items = $items . $cart_link;

return $items;
}

}

Here is the register code for the nav menus in functions.php

/*———————————————————————————–*/
/* Register WP3.0+ Menus
/*———————————————————————————–*/
register_nav_menu(‘Primary Navigation’, ‘Main Menu’);
register_nav_menu(‘Footer Navigation’, ‘Footer Menu’);
register_nav_menu(‘Top Toolbar Navigation’, ‘Top Toolbar Menu’);
//add nav_menu if UberMenu installed…
global $ttso;
$ubermenu = $ttso->ka_ubermenu;
if(‘true’ == $ubermenu):
register_nav_menu(‘UberMenu’, ‘UberMenu’);
endif;

I want to display the cart only on the main menu navigation at the top, and not below the image slider.

Read more here:: Append cart icon to end of main nav menu

Leave a Reply

Your email address will not be published. Required fields are marked *