WooCommerce is_cart() Function: Examples [Guide]

WooCommerce is_cart function guide with a shopping cart icon

Some WooCommerce code should only run on the cart page, such as loading a cart script or adding a body class for custom styling. Checking the page type first keeps that code from running everywhere else.

The woocommerce is_cart function, written as is_cart(), answers that question with a simple true or false. This guide explains what it checks, when to call it so it works, and how to use it in two practical examples.

What is_cart() Checks

is_cart() is a WooCommerce conditional tag. It returns true when the current request shows the cart page you set in WooCommerce, and false on every other page, including the checkout.

The check uses the page you chose in the settings, not the URL slug. You can see that choice under WooCommerce settings, Advanced tab, Page setup section. If you rename the cart page, the function still works. If you pick a different page as the cart, the function follows the new page.

Syntax and Return Value

  • Arguments: none.
  • Return value: true on the cart page, otherwise false.
  • Where to use it: inside hooks that run after the main query is set up.

When to Call is_cart()

Timing causes most failures. Conditional tags such as is_cart() depend on the main query, and WordPress sets that query up after the init hook. Calling the function earlier, for example directly in functions.php or on init, returns false even on the cart page.

Use one of these hooks instead:

  • wp fires after the main query is ready, so every conditional tag works.
  • template_redirect fires before the template loads, which suits logic that might redirect.
  • wp_enqueue_scripts fires while the page head is built, so it is the right place to load assets.

Tip: If is_cart() returns false on the cart page, check the hook first. Move the call into wp or wp_enqueue_scripts before you change anything else.

Example 1: Load a Script Only on the Cart Page

Loading assets only where they are needed keeps the rest of the site fast. This example enqueues a file only when is_cart() is true. Replace the path with your own file.

<?php

add_action( 'wp_enqueue_scripts', 'my_enqueue_cart_script' );

function my_enqueue_cart_script() {
    if ( ! is_cart() ) {
        return;
    }

    wp_enqueue_script(
        'my-cart-script',
        get_stylesheet_directory_uri() . '/js/cart.js',
        array( 'jquery' ),
        '1.0.0',
        true
    );
}

Put the code in a child theme’s functions.php file or in a small site plugin, so it survives theme updates.

Example 2: Add a Cart-Only Body Class

Body classes let you target the cart page in CSS without editing templates. This filter adds a custom class to the body tag on the cart page only.

<?php

add_filter( 'body_class', 'my_cart_body_class' );

function my_cart_body_class( $classes ) {
    if ( is_cart() ) {
        $classes[] = 'my-cart-page';
    }

    return $classes;
}

Then target the class in your stylesheet, for example .my-cart-page .cart-collaterals, to adjust the spacing around the totals.

Common Mistakes

  • Calling it too early. A check in init or at file load returns false. Move the call into a hook that runs after wp.
  • Wrapping cart-only hooks in an is_cart() check. Hooks such as woocommerce_before_cart already fire only on the cart page, so the extra check adds nothing.
  • Confusing the cart with the checkout. They are separate pages with separate tags. Use is_checkout() for the checkout page.
  • Matching the slug. is_page( 'cart' ) breaks when the slug changes or the page is translated. is_cart() uses the page ID that WooCommerce stores.

The other page checks follow the same pattern. Read the guides on is_checkout(), is_shop(), is_account_page(), and is_product(). For a cart rule that changes what customers can buy, see how to allow only one product in the cart.

FAQs About is_cart()

Does is_cart() return true on the checkout page?

No. The checkout is a separate page with its own check. Use is_checkout() for that page.

Does is_cart() work with the block-based cart?

Yes, as long as the page is set as the cart page in WooCommerce settings. Test on a staging copy first if you use a custom cart template.

Can I call is_cart() in functions.php?

You can call it, but only inside a hook that runs after the main query is ready, such as wp or wp_enqueue_scripts. A direct call at file load returns false.

What is the difference between is_cart() and is_page( ‘cart’ )?

is_page( ‘cart’ ) matches the slug. is_cart() matches the cart page that WooCommerce stores, so it stays correct after a slug change.

Conclusion

is_cart() is the simplest way to target the WooCommerce cart page. Call it from wp or wp_enqueue_scripts, and it returns true only on the cart.

If you need cart, checkout, or account changes built for your store, hire a WooCommerce developer or send your requirements through the application form.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.