News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Delete all products from Cart by one click

Started by Chifu, July 03, 2012, 19:43:29 PM

Previous topic - Next topic

Chifu

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
Quotepublic 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
Quotepublic function removeCart() {
   if (!empty($this->products)) {
      foreach ( $this->products as $key => $val ) {
         unset($this->products[$key]);
         $this->setCartIntoSession();
      }
      return true;
   }
}

Milbo


$cart = VirtueMartCart::getCart();
$cart->emptyCart();

should do the same, I mean instead of removeCart()
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/


ahaaaa

To get this working for me with SEO enabled required an additional line in the router.php

Look for

elseif ( $helper->compareKey($segments[0] ,'delete') ) $vars['task'] = 'delete' ;

and add an extra line below

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