Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Last active October 30, 2023 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/46ab54c2f270397be244cd385bd969a3 to your computer and use it in GitHub Desktop.
Save SiR-DanieL/46ab54c2f270397be244cd385bd969a3 to your computer and use it in GitHub Desktop.
Add an Empty Cart Button in WooCommerce
<?php
add_action( 'wp_loaded', 'woocommerce_empty_cart_action', 20 );
function woocommerce_empty_cart_action() {
if ( isset( $_GET['empty_cart'] ) && 'yes' === esc_html( $_GET['empty_cart'] ) ) {
WC()->cart->empty_cart();
$referer = wp_get_referer() ? esc_url( remove_query_arg( 'empty_cart' ) ) : wc_get_cart_url();
wp_safe_redirect( $referer );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment