Ask your WordPress questions! Pay money and get answers fast! (more info)

Adding Shopping Cart icon to end of Bootstrap menu WordPress

  • REFUNDED

I'm utilizing the Storefront Woocommerce shopping cart icon that when hovered over shows the items in the shopping cart. I'm trying to add this feature to display at the end of a bootstrap menu. I'm able to add an item to the end of the menu using this simple function:


function add_shop($menu, $args) {
if( 'primary' !== $args->theme_location )
return $menu;
return $menu . '<li class="right"><a href="">Shop</a></li>';
}
add_filter('wp_nav_menu_items','add_shop', 10, 2);


Now I'm trying to add the shop cart information in this location (at the end of the menu). I'm using this function in my site to show the cart and the cart items on hover:


if ( ! function_exists( 'storefront_header_cart' ) ) {
function storefront_header_cart() {
if ( is_woocommerce_activated() ) {
if ( is_cart() ) {
$class = 'current-menu-item';
} else {
$class = '';
}
?>
<ul class="site-header-cart menu">
<li class="<?php echo esc_attr( $class ); ?>">
<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
</li>
<li>
<?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
</li>
</ul>
<?php
}
}
}


It's working fine but not able to get it to show at the end of the menu. Thanks!

Answers (3)

2016-03-17

Andrea P answers:

have you tried something like this?


function add_shop($menu, $args) {

if( 'primary' !== $args->theme_location )

return $menu;

return $menu . '<li class="right">'. storefront_header_cart() .'</li>';

}

add_filter('wp_nav_menu_items','add_shop', 10, 2);


Lauren comments:

Hey Andrea

That seems to be functioning well, except it doesn't seem to be showing within the .navbar-nav class. I attached a screenshot.

Thanks!