EDD Cart Link With Item Count Shortcode
Adds a [wpide_edd_cart] shortcode that prints a "Cart (2)" link to the Easy Digital Downloads checkout, for headers, menus or widgets.
Opens WPIDE on
Code
add_shortcode( 'wpide_edd_cart', function ( $atts ) {
if ( ! function_exists( 'edd_get_cart_quantity' ) ) {
return '';
}
$atts = shortcode_atts( array( 'label' => 'Cart' ), $atts, 'wpide_edd_cart' );
$count = (int) edd_get_cart_quantity();
return sprintf(
'<a class="wpide-edd-cart-link" href="%1$s">%2$s <span class="edd-cart-quantity">(%3$d)</span><span class="screen-reader-text"> %4$s</span></a>',
esc_url( edd_get_checkout_uri() ),
esc_html( $atts['label'] ),
$count,
esc_html( sprintf( _n( '%d item', '%d items', $count ), $count ) )
);
} );
Notes
Use [wpide_edd_cart label="Basket"] to change the text; full-page caches may need to skip pages that show it.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.