Page 1 of 1

[MOD] Empty Cart button

Posted: Sun Jan 24, 2010 5:37 am
by goldndelicious
Hi All,

Can anyone suggest how to code a button, or link where by when clicked, it will empty the entire contents of the cart without the user having to got to the basket?

Ideally i'd like to place this in the cart module if possible.

Thanks

Re: Empty Cart button

Posted: Sun Jan 24, 2010 4:49 pm
by i2Paq
Have a look: Here.

Re: Empty Cart button

Posted: Sun Jan 24, 2010 5:21 pm
by dbstr
i2Paq wrote:Have a look: Here.
Sorry but how is that thread relevant?

Re: Empty Cart button

Posted: Sun Jan 24, 2010 6:04 pm
by dbstr
This will only work (and show up) if you got ajax cart enabled.

Edit: catalog/controller/module/cart.php

Find this (at the bottom of the file)

Code: Select all

}
?>
Right above it, insert:

Code: Select all

    public function clear() {
        $this->language->load('module/cart'); 
        
        if ($this->request->server['REQUEST_METHOD'] == 'POST') {
            if (isset($this->request->post['clearcart'])) { 
                $this->cart->clear();
                
                $output = '<div style="text-align: center;">' . $this->language->get('text_empty')  . '</div>';
        
                $this->response->setOutput($output, $this->config->get('config_compression'));
            }
        }
    } 
Edit: catalog/view/theme/yourtemplate/template/module/cart.tpl
Find

Code: Select all

<div class="bottom">&nbsp;</div> 
Right above it, insert:

Code: Select all

  <?php if ($ajax) { ?>   
  <div style="text-align: right; border-left: 1px solid #DDDDDD; border-right: 1px solid #DDDDDD;">
    <form action="<?php echo $this->url->http('checkout/cart'); ?>" method="post" enctype="multipart/form-data" id="clearcart"> 
        <input type="hidden" name="clearcart" value="1" />
        <a onclick="$('#clearcart').submit();" id="clearcart_button" class="button"><span><?php echo $this->language->get('text_clear'); ?></span></a>
    </form>
  </div>
  <?php } ?>
In the same file, find this (near the bottom of the file)

Code: Select all

});     
//--></script>
Right above it, insert:

Code: Select all

    $('#clearcart_button').replaceWith('<a onclick="" id="clearcart_button" class="button">' + $('#clearcart_button').html() + '</a>'); 
    
    $('#clearcart_button').click(function () {
        $.ajax({
            type: 'post',
            url: 'index.php?route=module/cart/clear',
            dataType: 'html',
            data: $('#clearcart :input'),
            success: function (html) {
                $('#module_cart .middle').html(html);
            },
        });            
    });
Edit: catalog/language/english/module/cart.php
Find

Code: Select all

$_['text_empty']    = '0 items';
Below it, insert:

Code: Select all

$_['text_clear']    = 'Clear';

Re: Empty Cart button

Posted: Sun Jan 24, 2010 6:20 pm
by KGal
Hi! dbstr

That is excellent! thanks very much for sharing, is that for 1.4 please?

Re: Empty Cart button

Posted: Sun Jan 24, 2010 6:35 pm
by dbstr
Yeah, it works with 1.4.0 :)

Re: Empty Cart button

Posted: Sun Jan 24, 2010 7:06 pm
by KGal
Ok! great thanks

Re: [MOD] Empty Cart button

Posted: Mon Jan 25, 2010 6:22 am
by goldndelicious
Thats fantastic thank you.

Just one question. How do i know if ajax is enabled? i don't really know much about ajax

Re: [MOD] Empty Cart button

Posted: Tue Jan 26, 2010 2:15 pm
by nzjollyroger
can this work with 1.3.2?

Re: [MOD] Empty Cart button

Posted: Fri Feb 05, 2010 6:07 am
by Miguelito
How could this mod be updated so it wouldn't show the 'Clear' button if there's no items in the cart?

Re: [MOD] Empty Cart button

Posted: Fri Feb 05, 2010 6:32 am
by dbstr
What I posted was just a veeeeeery quick and easy way of doing it, not a real mod as such... But I'll see about making it work properly, I'm probably gonna use it myself, someday... ;-)

Re: [MOD] Empty Cart button

Posted: Fri Feb 05, 2010 7:18 pm
by dbstr
Alright, new version.. Undo all the changes from the previous post, and try this:

Edit: catalog/view/theme/yourtemplate/template/module/cart.tpl
Find (line ~19):

Code: Select all

<div style="text-align: right;"><?php echo $text_subtotal; ?>&nbsp;<?php echo $subtotal; ?></div> 
Below, insert:

Code: Select all

<a onclick="clearCart();" class="button"><span><?php echo $this->language->get('text_clear'); ?></span></a>
--------------------------------------------------------------------------------------------------------------------------------
Find (at the bottom of the file, before <?php } ?> ):

Code: Select all

});     
//--></script>
Below, insert:

Code: Select all

<script type="text/javascript"><!--
function clearCart() {
    $.ajax({
        type: 'post',
        url: 'index.php?route=module/cart/clear',
        dataType: 'html',
        data: '&clearcart=true',
        success: function (html) {
            $('#module_cart .middle').html(html);
        },
    });            
}
//--></script>
--------------------------------------------------------------------------------------------------------------------------------
Edit: catalog/controller/module/cart.php
Find (line ~88):

Code: Select all

$output .= '<div style="text-align: right;">' . $this->language->get('text_subtotal') . '&nbsp;' .  $this->currency->format($this->cart->getTotal()) . '</div>'; 
Below, insert:

Code: Select all

$output .= '<a onclick="clearCart();" class="button"><span>' . $this->language->get('text_clear') . '</span></a>';
--------------------------------------------------------------------------------------------------------------------------------
Find (at the bottom of the file):

Code: Select all

}
?>
Above, insert:

Code: Select all

    public function clear() {
        $this->language->load('module/cart'); 
        
        if ($this->request->server['REQUEST_METHOD'] == 'POST') {
            if (isset($this->request->post['clearcart'])) { 
                $this->cart->clear();
                
                $output = '<div style="text-align: center;">' . $this->language->get('text_empty')  . '</div>';
        
                $this->response->setOutput($output, $this->config->get('config_compression'));
            }
        }
    } 
--------------------------------------------------------------------------------------------------------------------------------
Edit: catalog/language/english/module/cart.php
Find

Code: Select all

$_['text_empty']    = '0 items';
Below it, insert:

Code: Select all

$_['text_clear']    = 'Clear';

Re: [MOD] Empty Cart button

Posted: Sat Feb 06, 2010 5:40 am
by Miguelito
Tested in 1.4.0 and it's working great! Thank you :)

Re: [MOD] Empty Cart button

Posted: Sun Feb 07, 2010 12:10 am
by DannyMacD
i have done the above but got this error on shop:

Parse error: syntax error, unexpected T_PUBLIC in /home/disifin/public_html/shop/catalog/controller/module/cart.php on line 134

help please :)

Re: [MOD] Empty Cart button

Posted: Sun Feb 07, 2010 1:12 am
by dbstr
could your paste your cart.php?

Re: [MOD] Empty Cart button

Posted: Sun Feb 07, 2010 1:36 am
by DannyMacD
never mind me being special...

works a treat!!

thank you!

Re: [MOD] Empty Cart button

Posted: Sun Feb 07, 2010 4:37 am
by The Alchemist
Nice Mod

Re: [MOD] Empty Cart button

Posted: Thu Mar 17, 2011 4:59 am
by RJonesUSC
Anyone have this working on the latest release? I can't seem to get it working on 1.4.9.3.

Re: [MOD] Empty Cart button

Posted: Thu Oct 04, 2012 2:53 am
by alexgg
Hi dbstr and others,

Thanks for the nice adjustment.
What I was wondering, would it be possible with only the adjustment in cart.php as described by dbstr above, to just have the clearcart done by adding a parameter to the URL? So e.g. by requesting this:

/cart/index.php?route=checkout/cart&clearcart

??

This would be really really cool, cause I couldn't find another way of Opencart doing this.
Thanks for anyone having a suggestion.
Alex

P.S. I tried using the query above, but it didn't work...maybe I have to append it in someway?

Re: [MOD] Empty Cart button

Posted: Tue Nov 11, 2014 7:05 am
by MinoS
You may be interested in my Clear Cart Button extension http://www.opencart.com/index.php?route ... n_id=16587

Also I provide free support :)