Last active
July 26, 2023 11:10
-
-
Save luizbills/9bb88cb269728e746ce7e110ea1945cf to your computer and use it in GitHub Desktop.
Button for clear/empty cart in WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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