clear the cart on add product

  • Posts: 27
  • Thank you received: 0
12 years 11 months ago #39481

Hi,
I have a digital product that have 3 versions (with different prices)
I already configured the product with the vars.
I dont want to allow my customer to select quantity of product (like you did) so I removed it from the product page and the cart page.
The problem is, if a user decide to go back and add to cart again, he will have 2 quantity (same as if I click add Essential product, then go to the product page again and click add again, I will have 2 quantity), so I limited the quantity to 1 and the item quantity to 1, so only one product could be bought at the same time.
The problem is, if a user decide to go back and select a different version, and click on add to cart, the old version is still there.
I'm looking for a way to clear the cart every time an item is added to it, so the most resent item will be in the cart.

Thanks.

Last edit: 12 years 11 months ago by justaguy.

Please Log in or Create an account to join the conversation.

  • Posts: 27
  • Thank you received: 0
12 years 11 months ago #39633

any one can help?

Please Log in or Create an account to join the conversation.

  • Posts: 27
  • Thank you received: 0
12 years 11 months ago #39637

Thanks for not answering, learned a lot in the way.... :)
here is the way to do it, be advised this is hard coding for the system so it may break on updates and such, always keep a change log for changes you make!!!
this is really simple, please , hika support, if this is not correct let me know.
you should edit this file: administrator/components/com_hikashop/classes/cart.php
just insert this code:

$this->resetCart(true);
at line 198 just after this code:
function update($product_id,$quantity=1,$add=0,$type='product',$resetCartWhenUpdate=true,$force=false){
That worked for me, and now every time an item is added the cart will reset.

Please Log in or Create an account to join the conversation.

  • Posts: 296
  • Thank you received: 5
  • Hikashop Business
12 years 11 months ago #39645

Thought occured to me that a solution using a plugin might be more flexible and of a wider interest.
Something like this at line 208 (not tried it - just a suggestion for further consideration).

JPluginHelper::importPlugin( 'hikashop' );
$dispatcher =& JDispatcher::getInstance();
if (!$dispatcher->trigger( 'onBeforeUpdateCart', $this, $cart, $product_id, $quantity, $add, $type )) return false;

In this case the resetCart() call (followed by return true) would go into the plugin.

For consistency an 'onAfterUpdateCart' trigger might be worth including after line 264.

Last edit: 12 years 11 months ago by brainforge. Reason: Will need $this and $cart passed into trigger.

Please Log in or Create an account to join the conversation.

  • Posts: 27
  • Thank you received: 0
12 years 11 months ago #39651

Thanks for your help, I was hopping I can make a plugin that will not edit the core files...
Will look in to it (as I never built a plugin before) as soon as I have time.... :)

Please Log in or Create an account to join the conversation.

  • Posts: 83103
  • Thank you received: 13416
  • MODERATOR
12 years 11 months ago #39684

Indeed, since there is no trigger on that at the moment your solution is good justaguy.

However, the idea of brainforge is great.
We'll add that to next release so you can go ahead and make it a hikashop plugin with the code proposed by brainforge.

Note that we'll change the trigger name to onBeforeCartUpdate in order to be consistent with the name of triggers already in HikaShop.

Last edit: 12 years 11 months ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 27
  • Thank you received: 0
12 years 11 months ago #39843

I wish I know how to make plugins, think it would have made my life much easier.
Just wanted to let you know I have edited my code as it had a "bug" when I entered coupon on the cart was also cleared.
so i just added a check to my line:

if($type=='product')$this->resetCart(true);

Please Log in or Create an account to join the conversation.

  • Posts: 83103
  • Thank you received: 13416
  • MODERATOR
12 years 11 months ago #39998

Making plugins for joomla ecosystem is not complicated. You can use the files of the hikashop history plugin as base if you want.
They are in the folder plugins/hikashop on your website. Open them and change all the names of the plugin (xml, class name, etc), then put your code, zip the package and you can then install it via the joomla installer.

The following user(s) said Thank You: justaguy

Please Log in or Create an account to join the conversation.

  • Posts: 27
  • Thank you received: 0
12 years 11 months ago #40445

Hi Nicolas,
I'm looking to build this plug-in now, can you help me do it with the fix i added at the end?
I have copied the history plugin and renamed it (and the files inside) to clear_cart.
the code on the xml file:

<files>
		<filename plugin="clear_cart">clear_cart.php</filename>
	</files>
	<params addpath="/components/com_hikashop/params">
	</params>
the code on the php file:
class plgHikashopClear extends JPlugin
{
	function plgHikashopClear(&$subject, $config){
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin =& JPluginHelper::getPlugin('hikashop', 'clear_cart');
			jimport('joomla.html.parameter');
			$this->params = new JParameter( $plugin->params );
		}
    }
    function onBeforeCartUpdate(){
		if($type=='product')$this->resetCart(true);
return true;
    }
}
is this the right way?

Last edit: 12 years 11 months ago by justaguy.

Please Log in or Create an account to join the conversation.

  • Posts: 83103
  • Thank you received: 13416
  • MODERATOR
12 years 10 months ago #40623

You class name should be:
plgHikashopClear_cart as well as the constructor function name.

Please Log in or Create an account to join the conversation.

  • Posts: 27
  • Thank you received: 0
12 years 10 months ago #40656

thanks, this is my files now:
in cart.php i have removed my code and inserted this one instead on line 208 just before the closing of this if: if($this->cart->cart_id){

JPluginHelper::importPlugin( 'hikashop' );
$dispatcher =& JDispatcher::getInstance();
if (!$dispatcher->trigger( 'onBeforeCartUpdate', $this, $cart, $product_id, $quantity, $add, $type )) return false;
I have also tried to put it after the closing }, still didn't worked...

this is my clear_cart.php file:
<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.5.6
 * @author		hikashop.com
 * @copyright	(C) 2010-2012 HIKARI SOFTWARE. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgHikashopClear_cart extends JPlugin
{
	function plgHikashopClear_cart(&$subject, $config){
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin =& JPluginHelper::getPlugin('hikashop', 'Clear_cart');
			jimport('joomla.html.parameter');
			$this->params = new JParameter( $plugin->params );
		}
    }
    function onBeforeCartUpdate(){
		if($type=='product')$this->resetCart(true);
		return true;
    }
}

what am i doing wrong?

Last edit: 12 years 10 months ago by justaguy.

Please Log in or Create an account to join the conversation.

  • Posts: 83103
  • Thank you received: 13416
  • MODERATOR
12 years 10 months ago #40883

Please make sure that your plugin is published in the joomla plugins manager.
If you don't see there, it means that you didn't install it. In that case, zip the files of the plugin and install the zip via the extension installer of Joomla. Then you will be able to publish it.

Please Log in or Create an account to join the conversation.

Time to create page: 0.089 seconds
Powered by Kunena Forum