WordPress.org

Ready to get started?Download WordPress

Forums

Add an empty cart button to cart page (7 posts)

  1. idealneal
    Member
    Posted 1 year ago #

    I want to add a clear cart button next to the update cart button on the cart page.
    I added this code to the functions file:

    // check for clear-cart get param to clear the cart
    add_action( 'init', 'woocommerce_clear_cart_url' );
    function woocommerce_clear_cart_url() {
    if ( isset( $_GET['clear-cart'] ) ) {
    global $woocommerce;
    $woocommerce->cart->empty_cart();
    }
    }

    and I added this code to cart.php just before the update button:

    <input type="submit" class="button" name="clear-cart" value="<?php _e('Empty Cart', 'woocommerce'); ?>" />

    Nothing shows up. Obviously I have done something wrong. I know just enough PHP to be dangerous. If anyone could help I sure would appreciate it.

  2. derekseymour
    Member
    Posted 1 year ago #

    Together with the following Empty Cart button code in cart.php:

    <input type="submit" class="button" name="clear-cart" value="<?php _e('Empty Cart', 'woocommerce'); ?>" />

    try adding this to your functions.php file instead, which checks for both GET and POST form submissions:

    add_action('init', 'woocommerce_clear_cart_url');
    function woocommerce_clear_cart_url() {
    	global $woocommerce;
    	if( isset($_REQUEST['clear-cart']) ) {
    		$woocommerce->cart->empty_cart();
    	}
    }
  3. shnize
    Member
    Posted 1 year ago #

    I am trying to do this but with no luck. I don't exactly have just a cart.php page that I can see. I have a bunch of pages that have the word cart in it but not one specifically just cart. php.

    Any help would be greatly appreciated.

  4. WebEndev
    Member
    Posted 1 year ago #

    Works great derekseymour
    Thanks!

  5. johnlanglois
    Member
    Posted 1 year ago #

    Shnize,
    The cart.php file is under the cart folder, under templates.
    I put it in about line 135.
    Just keep it out of the php stream.

    <input type="submit" class="button" name="clear-cart" value="<?php _e('Empty Cart', 'woocommerce'); ?>" />
    <?php do_action('woocommerce_proceed_to_checkout'); ?>

    This does beg the question why isn't there a do_action woocommerce_empty_cart?

  6. João G
    Member
    Posted 10 months ago #

    derekseymour, worked like a charm! thanks man.

  7. khromov
    Member
    Posted 9 months ago #

    Thanks a lot derekseymour!

Topic Closed

This topic has been closed to new replies.

About this Topic