Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.