Skip to content

Instantly share code, notes, and snippets.

@luizbills
Last active July 26, 2023 11:10
Show Gist options
  • Save luizbills/9bb88cb269728e746ce7e110ea1945cf to your computer and use it in GitHub Desktop.
Save luizbills/9bb88cb269728e746ce7e110ea1945cf to your computer and use it in GitHub Desktop.
Button for clear/empty cart in WooCommerce
<?php
add_action( 'woocommerce_cart_coupon', 'lpb_woocommerce_empty_cart_button' );
function lpb_woocommerce_empty_cart_button () {
$cart_url = add_query_arg( 'empty_cart', 'yes', wc_get_cart_url() );
$label = 'Empty cart';
echo '<a href="' . esc_url( $cart_url ) . '" class="button">' . $label . '</a>';
}
add_action( 'wp_loaded', 'lpb_woocommerce_empty_cart_action', 20 );
function lpb_woocommerce_empty_cart_action () {
$empty_cart = $_GET['empty_cart'] ?? '';
if ( 'yes' === $empty_cart && function_exists( 'WC' ) ) {
WC()->cart->empty_cart();
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment