cart menu

Hi Movin,

is it possible to have a cart in navigation menu that change as soon as product is added into cart?

I have tried to do so by adding the following code into functions.php, but the problem is I have to refresh the page before the actual number of product is shown in cart navigation menu.

/**
 * Place a cart icon with number of items and total cost in the menu bar.
 */
function activello_woomenucart($menu, $args) {

	// Check if WooCommerce is active and add a new item to a menu assigned to Primary Navigation Menu location
	if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'primary' !== $args->theme_location )
		return $menu;

	ob_start();
		global $woocommerce;
		$viewing_cart = __('View your shopping cart', 'activello');
		$start_shopping = __('Start shopping', 'activello');
		$cart_url = $woocommerce->cart->get_cart_url();
		$shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
		$cart_contents_count = $woocommerce->cart->cart_contents_count;
		$cart_contents = sprintf(_n('%d item', '%d items', $cart_contents_count, 'activello'), $cart_contents_count);
		$cart_total = $woocommerce->cart->get_cart_total();
		// Uncomment the line below to hide nav menu cart item when there are no items in the cart
		//if ( $cart_contents_count > 0 ) {
			if ($cart_contents_count == 0) {
				$menu_item = '<li class="menu-item"><a class="woo-menu-cart" href="'. $shop_page_url .'" title="'. $start_shopping .'">';
			} else {
				$menu_item = '<li class="menu-item"><a class="woo-menu-cart" href="'. $cart_url .'" title="'. $viewing_cart .'">';
			}

			$menu_item .= '<i class="fa fa-shopping-cart"></i> ';

			$menu_item .= $cart_contents;
			$menu_item .= '</a></li>';
		// Uncomment the line below to hide nav menu cart item when there are no items in the cart
		//}
		echo $menu_item;
	$social = ob_get_clean();
	return $menu . $social;

}
add_filter('wp_nav_menu_items','activello_woomenucart', 10, 2);

My website is http://zharasonline.com/ in case you need this information.

Thanks!!

Hi @fadill,

I hope you are well today and thank you for your question.

You can try achieving this by using the following plugin.

Best Regards,
Movin

Hi Movin,

My purpose to remove the search bar in navigation menu with cart.
I know the location of the search bar in source, and I should modify this in header.php in child theme.
Can you give some clue to implement this?

Thanks!

The following code added in the header.php file of theme o line number 48 displays that search bar so you can modify this in header.php in child theme.


<div class="nav-search"><?php 
                            add_filter('get_search_form', 'activello_header_search_filter',10,3);
                            echo get_search_form();
                            remove_filter('get_search_form', 'activello_header_search_filter');?>							
</div>