Author Topic: Delete all products from Cart by one click  (Read 3984 times)

Chifu

  • Beginner
  • *
  • Posts: 3
Delete all products from Cart by one click
« on: July 03, 2012, 19:43:29 pm »
The task was as follows: clear cart by one click. Maybe this function will prove useful, or someone can do this better.


I added link in site/component/com_virtuemart/cart/default_pricelist.php
Quote
<a href="<?=JRoute::_( 'index.php?option=com_virtuemart&view=cart&task=deleteCart' ) ?>">Clear cart</a>

Function in site/component/com_virtuemart/controllers/cart.php
Quote
public function deleteCart() {
   $mainframe = JFactory::getApplication();
   $cart = VirtueMartCart::getCart();
   if ($cart->removeCart()) {
      $mainframe->enqueueMessage('All is done.');
   } else {
      $mainframe->enqueueMessage('Error with deleting.', 'error');
   }
   $mainframe->redirect(JRoute::_('index.php?option=com_virtuemart&view=cart'));
}

Function in site/component/com_virtuemart/helpers/cart.php
Quote
public function removeCart() {
   if (!empty($this->products)) {
      foreach ( $this->products as $key => $val ) {
         unset($this->products[$key]);
         $this->setCartIntoSession();
      }
      return true;
   }
}

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10018
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Delete all products from Cart by one click
« Reply #1 on: July 04, 2012, 01:52:11 am »
Code: [Select]
$cart = VirtueMartCart::getCart();
$cart->emptyCart();
should do the same, I mean instead of removeCart()
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

DATURA

  • Beginner
  • *
  • Posts: 6
Re: Delete all products from Cart by one click
« Reply #2 on: December 25, 2012, 19:52:05 pm »
Thanks! Works!

ahaaaa

  • Beginner
  • *
  • Posts: 14
Re: Delete all products from Cart by one click
« Reply #3 on: February 20, 2013, 11:05:16 am »
To get this working for me with SEO enabled required an additional line in the router.php

Look for
 
Code: [Select]
elseif ( $helper->compareKey($segments[0] ,'delete') ) $vars['task'] = 'delete' ;
and add an extra line below

Code: [Select]
elseif ( $helper->compareKey($segments[0] ,'deleteCart') ) $vars['task'] = 'deleteCart' ;
I'm no expert on these things and took me hours to resolve it. It may help others