Skip to content

Add a Clear Cart button to the cart page

By default, customers empty their Sunshine Photo Cart cart by removing each item one at a time. If your clients often build large carts while browsing and then want to start over, a single Clear Cart button saves them the clicks.

This snippet adds a Clear Cart button below the cart on the cart page. When clicked, it empties the entire cart and reloads the page.

Add the following custom code to your site:

// Empty the cart when the Clear Cart button is clicked
add_action( 'init', 'sunshine_clear_cart_request' );
function sunshine_clear_cart_request() {
    if ( isset( $_GET['sunshine_clear_cart'] ) && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'sunshine_clear_cart' ) ) {
        SPC()->cart->empty_cart();
        SPC()->notices->add( __( 'Cart cleared', 'sunshine-photo-cart' ) );
        wp_safe_redirect( sunshine_get_page_permalink( 'cart' ) );
        exit;
    }
}

// Show the Clear Cart button below the cart
add_action( 'sunshine_after_cart_form', 'sunshine_clear_cart_button' );
function sunshine_clear_cart_button() {
    $clear_url = wp_nonce_url(
        add_query_arg( 'sunshine_clear_cart', 1, sunshine_get_page_permalink( 'cart' ) ),
        'sunshine_clear_cart',
        'nonce'
    );
    // Change 'Clear Cart' below to rename the button
    echo '<div id="sunshine--cart--clear-button"><a href="' . esc_url( $clear_url ) . '" class="sunshine--button-alt button button-alt">' . esc_html__( 'Clear Cart', 'sunshine-photo-cart' ) . '</a></div>';
}

Learn how to add this custom code to your WordPress website

The button only appears when the cart has items (this part of the page isn't shown for an empty cart), and clearing shows a "Cart cleared" message so the customer knows it worked.

Still need help?

If you have not yet found your answer in the documentation articles, please contact support

Sunshine Photo Cart for WordPress