• Resolved atomonelone

    (@atomonelone)


    I have a function that lets me empty the cart with a click of a button. Since the latest update (I belive) this is not working anymore. I’ve been looking in the changelog but can’t really find the reason as to why it’s not working anymore. Is there something I’ve missed? Any help would be greatly appreciated.

    This is the function:

    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);
        }
    }
Viewing 1 replies (of 1 total)
  • Roxy

    (@roxannestoltz)

    Hi @atomonelone ,

    I understand that your previous code is no longer working, which allows the function “Empty Cart”.

    Please see if the following code works for you.

    I have tested this on my staging site which uses the latest version of WordPress and WooCommerce, and has the default Storefront theme active, and it is working as expected:

    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 );
    	}
    }

    Hope this helps ??

Viewing 1 replies (of 1 total)
  • The topic ‘Empty cart button not working.’ is closed to new replies.