“Empty cart” button
Adds an “Empty cart” button to the cart page that removes everything in one click.
<?php
add_action(
'woocommerce_cart_actions',
static function () {
printf(
'<a class="button snipfire-empty-cart" href="%s">%s</a>',
esc_url( wp_nonce_url( add_query_arg( 'snipfire-empty-cart', '1', wc_get_cart_url() ), 'snipfire-empty-cart' ) ),
esc_html__( 'Empty cart', 'woocommerce' )
);
}
);
add_action(
'wp_loaded',
static function () {
if ( isset( $_GET['snipfire-empty-cart'], $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'snipfire-empty-cart' ) && WC()->cart ) {
WC()->cart->empty_cart();
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
);
Good to know
For the classic cart and checkout (the [woocommerce_cart] and [woocommerce_checkout] shortcodes). The block cart and checkout don’t use these hooks.
In these packs
- WooCommerce: cart and checkout: Free-shipping progress, coupon links, quantity limits and a cleaner checkout.