WooCommerce Cart Shortcode: Display Your Cart Anywhere

WooCommerce Cart Shortcode: Display Your Cart Anywhere

The cart page on a default WooCommerce install works because of one shortcode sitting in the page content. But “display my cart anywhere” usually means something different: a mini cart in a sidebar or header, not the full cart page rebuilt somewhere else. Here’s both.

The Code

To render the full cart page (items, quantities, coupon field, cart totals), add this shortcode to any page’s content:

[woocommerce_cart]

If what you actually want is a compact cart summary outside the cart page itself, like a sidebar widget or a dropdown in the header, that’s a different job. Here’s a snippet that registers a shortcode showing item count and subtotal, usable anywhere:

add_shortcode( 'fixelar_mini_cart', 'fixelar_render_mini_cart' );

function fixelar_render_mini_cart() {
    if ( ! function_exists( 'WC' ) || is_null( WC()->cart ) ) {
        return '';
    }

    $count    = WC()->cart->get_cart_contents_count();
    $subtotal = WC()->cart->get_cart_subtotal();

    ob_start();
    ?>
    <div class="fixelar-mini-cart">
        <a href="<?php echo esc_url( wc_get_cart_url() ); ?>">
            <?php echo esc_html( $count ); ?> items
            (<?php echo wp_kses_post( $subtotal ); ?>)
        </a>
    </div>
    <?php
    return ob_get_clean();
}

Use it anywhere with [fixelar_mini_cart], including inside a widget area that supports shortcodes.

Where to Put This Code

The [woocommerce_cart] shortcode goes directly into the page content of whichever page you’re using as the cart page, not into functions.php.

The fixelar_mini_cart shortcode is PHP, so it belongs in your child theme’s functions.php or a snippets plugin like Code Snippets. Keep custom code out of a parent theme’s files, since a theme update will erase it.

If your store is on WooCommerce 8.3 or later, new installs default to the Cart Block instead of the shortcode. The shortcode still works, but if you’re editing the Cart page in the block editor, you’ll see a Cart block rather than a shortcode placeholder. To go back to the classic shortcode version, select the Cart block, click Transform in the block toolbar, and choose Classic Shortcode.

How the Code Works, Line by Line

WC()->cart is the global cart object. Checking is_null( WC()->cart ) first avoids a fatal error if this shortcode somehow renders before WooCommerce has initialized the cart, which can happen in certain caching or AJAX contexts.

get_cart_contents_count() returns the total quantity of items in the cart, not the number of distinct products.

get_cart_subtotal() returns a pre-formatted price string, already run through WooCommerce’s currency formatting, which is why it’s escaped with wp_kses_post() instead of esc_html(). The subtotal includes HTML markup for currency styling, and esc_html() would strip that out.

wc_get_cart_url() returns the URL of whichever page is assigned as the cart page in WooCommerce settings, so the link stays correct even if that page gets renamed or moved.

Real Use Cases

A sidebar or header mini cart on a custom theme. Not every theme includes a cart icon out of the box. This shortcode approach gets a working cart summary onto any page without a dedicated mini cart plugin.

Showing cart contents inside a landing page built with a page builder. Landing pages built outside the normal template flow often lose access to theme-level cart widgets. A shortcode dropped into a text or shortcode block sidesteps that.

A cart page that got wiped during a redesign. The same troubleshooting pattern as checkout: if the cart page is blank, check whether the shortcode is still there and whether the page is still assigned as the Cart page under WooCommerce → Settings → Advanced.

Multilingual stores needing the cart shortcode on translated pages. Each language version of the cart page needs its own [woocommerce_cart] shortcode and its own page assignment in the relevant translation plugin’s settings.

Common Issues

The cart page shows nothing. Confirm the page is assigned as the Cart page under WooCommerce → Settings → Advanced → Page setup. Like checkout, the shortcode alone isn’t enough.

The mini cart shortcode shows a blank space instead of an error. That’s the is_null() check doing its job. If it’s returning empty consistently, WooCommerce likely isn’t fully loaded when the shortcode runs, often because it’s being called too early in a page builder’s execution order.

Cart totals don’t update without a page refresh. This shortcode renders a static snapshot on page load. Making it update live after an AJAX add-to-cart requires hooking into WooCommerce’s added_to_cart JavaScript event and re-rendering the fragment, which is a bigger job than the shortcode itself.

The block editor won’t let me paste the shortcode directly. In newer WooCommerce versions, adding [woocommerce_cart] to a Paragraph block on a block theme may render as literal text instead of the cart. Use a Shortcode block specifically, or the Classic Shortcode transform described above.

If you’d rather have a fully live-updating mini cart built and tested against your theme, that’s exactly the kind of work Fixelar handles as part of our WooCommerce custom development services. We build it once, test it against your actual plugin stack, and it stays working through updates.