Adding Easy Digital Download Cart Quantity to Menu

March 22, 2015

When using the popular e-commerce plugin Easy Digital Downloads it is very common you will want to add the cart quantity to the menu. There are a few ways to do this but I have found adding it to the wp_nav_menu is great for styling consistency and maintainability.

First lets look at how to get the cart quantity.


<a href="<?php echo edd_get_checkout_uri(); ?>">
    Cart (<span class="edd-cart-quantity"><?php echo edd_get_cart_quantity(); ?></span>)
</a>

The above snippet links to the checkout page. It will also update the quantity via ajax if ajax is enabled in Easy Digital Downloads and the edd-cart-quantity class is present.

The above snippet works well for general use, but a few things need to be adjusted to add it to the menu.

By utilising the items_wrap parameter that wp_nav_menu has we need to create a little function to pass in our cart quantity link so all the PHP functions output correctly.


<?php

function wdwnavwrap() { $newnavwrap = ’<ul id=“%1$s” class=“%2$s”>’; $newnavwrap .= ‘%3$s’; $newnavwrap .= ’<li class=“menu-item”>’; $newnavwrap .= ’<a href=”’ . eddgetcheckouturi() . ‘”>’; $newnavwrap .= ‘Cart (<span class=“edd-cart-quantity”>’ . eddgetcartquantity() .’</span>)’; $newnavwrap .= ’</a>’; $newnavwrap .= ’</li>‘; $newnavwrap .= ’</ul>‘;

return $new_nav_wrap;

}

?>

And then we need to add that to our wp_nav_menu like so:


<?php wp_nav_menu( array(
    'theme_location' => 'primary',
    'menu_class' => 'main-menu',
    'items_wrap' => wdw_nav_wrap()
    ) ); 
?>