• Hello. I have some problem to add the plugin WooCommerce Cart Count Shortcode to top menu . I want to have this somewhere next to shop link on that top menu (next to main logo)

Viewing 2 replies - 1 through 2 (of 2 total)
  • Theme Author silverks

    (@silverks)

    You can the cart count to menu item without any plugins using the following code (add to your child theme’s functions.php file):

    
    /**
     * Add the cart quantity to cart link in header menu
     */
    function franz_child_cart_quantity( $item_output, $item, $depth, $args ){
    	$item_classes = get_post_meta( $item->ID, '_menu_item_classes', true );
    	if ( ! in_array( 'your-menu-item-class', $item_classes ) ) return $item_output;
    	
    	$cart_count = WC()->cart->get_cart_contents_count();
    	if ( $cart_count > 0 ) $item_output = preg_replace( '/(<a\s[^>]*[^>]*>)(.*)(<\/a>)/siU', '$1$2 <span class="cart-count">' . $cart_count . '</span>$3', $item_output );
    	
    	return $item_output;
    }
    add_filter( 'walker_nav_menu_start_el', 'franz_child_cart_quantity', 20, 4 );
    

    Replace your-menu-item-class in the code above with any class you assign to the menu item where the cart count should appear.

    Thread Starter blazejwiecha

    (@blazejwiecha)

    Ok so I put this code to functions.php , changed your-menu-item-class with menu-item-7 but still there is nothing shoving on menu

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding WooCommerce Cart Count Shortcode to top menu’ is closed to new replies.