Skip to content

Instantly share code, notes, and snippets.

@met-eric
Last active November 22, 2022 18:46
Show Gist options
  • Save met-eric/4143dd15f2e4524c85fe6a8cc3687031 to your computer and use it in GitHub Desktop.
Save met-eric/4143dd15f2e4524c85fe6a8cc3687031 to your computer and use it in GitHub Desktop.
Add WooCommerce Empty Cart Button.php
add_action( 'woocommerce_cart_coupon', 'custom_woocommerce_empty_cart_button' );
function custom_woocommerce_empty_cart_button() {
echo '<a href="' . esc_url( add_query_arg( 'empty_cart', 'yes' ) ) . '" class="button" title="' . esc_attr( 'Empty Cart', 'woocommerce' ) . '">' . esc_html( 'Empty Cart', 'woocommerce' ) . '</a>';
}
add_action( 'wp_loaded', 'custom_woocommerce_empty_cart_action', 20 );
function custom_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