Empty cart button not working
-
Why does this code does not work? it just reload the page without clearing the woodmart cart
Here is where i found the code: https://metorik.com/blog/how-to-add-an-empty-cart-button-to-your-woocommerce-store
add_action( 'woocommerce_cart_actions', '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 );
}
}
-
This topic was modified 3 years, 7 months ago by
kilimats.
Hello,
Our support does not cover testing and fixing third parties codes. Please let us know what is your aim, perhaps we would be able to suggest a solution.
Best Regards