MarketPress mini cart as the menu item with products count

I want to have the mini cart display as a menu item. In the main menu on my website, I currently have a 'cart' button.
I want that cart button to display the number of items in the card (if any) similar the Mini-cart floating bar. Is this possible?

  • Ash

    Hello Scott Taylor

    Would you please try the following code?

    add_filter( 'wp_nav_menu_items', 'add_cart_link', 10, 2 );
    function add_cart_link( $items, $args ) {
    	global $mp;
    	$items .= '<li class="menu_cart">'.mp_cart_link( false, false, 'Cart (<span>' . get_cart_value(false) . '</span>)' ).'</li>';
    	return $items;
    }
    
    add_action( 'wp_footer', 'add_cart_value' );
    function add_cart_value() {
    	global $mp;
    	$cart = $mp->get_cart_cookie();
    	?>
    	<script type="text/javascript">
    		jQuery(function($){
    			$(document).on('submit', '.mp_buy_form:has(input[name="action"])', function(e) {
    				e.preventDefault();
    				setTimeout(function() {
    					$.post('<?php echo admin_url( 'admin-ajax.php' ); ?>', {action: 'get_cart_value'}, function(response) {
    						$('.menu_cart a span').text(response);
    					});
    				}, 2000)
    			});
    		});
    	</script>
    	<?php
    }
    
    add_action( 'wp_ajax_get_cart_value', 'get_cart_value' );
    function get_cart_value( $echo = true ) {
    	global $mp;
    	$total = 0;
    	$cart = $mp->get_cart_cookie();
    	foreach( $cart as $c ){
    		$total += $c[0];
    	}
    	if( $echo ){
    		echo $total;
    		die();
    	}else{
    		return $total;
    	}
    }

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    This may vary on different themes, so let's give a try and see how it works with your theme. Let us know how it goes.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.