Empty Cart Button
Adds an "Empty cart" button next to "Update cart" on the classic cart page, protected by a nonce.
Opens WPIDE on
Code
add_action( 'woocommerce_cart_actions', function () {
$url = wp_nonce_url( add_query_arg( 'wpide_empty_cart', '1', wc_get_cart_url() ), 'wpide_empty_cart' );
printf( '<a class="button wpide-empty-cart" href="%s">Empty cart</a>', esc_url( $url ) );
} );
add_action( 'wp_loaded', function () {
if ( empty( $_GET['wpide_empty_cart'] ) || ! function_exists( 'WC' ) || ! WC()->cart ) {
return;
}
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_key( wp_unslash( $_GET['_wpnonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'wpide_empty_cart' ) ) {
return;
}
WC()->cart->empty_cart();
wp_safe_redirect( wc_get_cart_url() );
exit;
}, 20 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.