L
L
leviathan2017-07-31 15:08:18
PHP
leviathan, 2017-07-31 15:08:18

Empty cart button in woocimmerce?

You need to make a button to empty the basket. Tried to do so

add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;

if ( isset( $_GET['empty-cart'] ) ) {
$woocommerce->cart->empty_cart();
}
}

<a class="button" href="<?php echo $woocommerce->cart->get_cart_url(); ?>?empty-cart"><?php _e( 'Empty Cart', 'woocommerce' ); ?>

And the page stops loading.
Are there any alternatives? Preferably without a plugin.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mr Crabbz, 2017-07-31
@The_Lars

Button:
- required in the form tag. Make sure the form is not nested inside another form
Code for functions.php:

add_action('init', 'woocommerce_clear_cart_url');
function woocommerce_clear_cart_url() {
    global $woocommerce;
    if( isset($_REQUEST['clear-cart']) ) {
        $woocommerce->cart->empty_cart();
    }
}

Copied from my current project - everything works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question