# Redirect an Empty Cart to the Shop

> Sends visitors who open the cart page with nothing in it straight to the shop page instead of showing an empty cart.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, cart, redirect
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-redirect-empty-cart/

## Code

```php
add_action( 'template_redirect', function () {
    if ( ! function_exists( 'is_cart' ) || ! is_cart() || ! WC()->cart || ! WC()->cart->is_empty() ) {
        return;
    }

    $target = wc_get_page_permalink( 'shop' ); // Where empty carts go.

    wp_safe_redirect( $target );
    exit;
} );
```

## Notes

Change $target to redirect to another page such as a category or the homepage.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect an Empty Cart to the Shop" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
