Clear cart before a certain product is added

  • Posts: 13
  • Thank you received: 2
10 years 3 months ago #167912

-- HikaShop version -- : current
-- Joomla version -- : current 3.3
-- PHP version -- : current

hi!

I would like to create a plugin that does something when a product is added to the cart:

- It should check whether the product is part of a certain category. If this is the case, the plugin should empty the cart and then add the product to the cart, making it the only product in the cart

- If a certain product is added to the cart, it should change the shop's currency

Right now, I am working on the first part, but I do not even manage to make the cart get cleared for every product added. What I tried is this:

function onBeforeCartUpdate(&$cartClass,&$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force,&$do)
	{
 		if (($type=='product') && ($add=1)) $cartClass->resetCart();
 		$do=true;
		return true;
	}

It clears the cart, but the product is not added afterwards. Can somebody please guide me on what I am doing wrong here? Additionally, I would greatly appreciate if you could tell me how to check if a certain product is part of a certain category.

Thank you very much & all the best,

Lukas

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

  • Posts: 82864
  • Thank you received: 13372
  • MODERATOR
10 years 3 months ago #167948

Hi,

If you reset the cart in the onBeforeCartUpdate function, you need to re-init it or the system won't be able to add the products to the cart after that function.
So adding that line in your code after the reset will probably help:
$cart = $cartClass->initCart();

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

  • Posts: 13
  • Thank you received: 2
10 years 3 months ago #168060

Hi!

Thank you, it kinda works! Only problem is, if I add the same product twice, it just says "The cart is empty" - do you have any idea what could be the problem here?

Cheers!

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

  • Posts: 82864
  • Thank you received: 13372
  • MODERATOR
10 years 3 months ago #168082

Hi,

I don't know. That would require adding traces in the file administrator/components/com_hikashop/classes/cart and especially in the updateEntry function to see what data is passing by which is different from the first time to the second time in order to understand what is missing.

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

  • Posts: 13
  • Thank you received: 2
10 years 3 months ago #168322

I am kinda stuck, do you have any other ideas how it could be possible to clean the cart in the onBeforeCartUpdate function?

thanks!!

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

  • Posts: 13201
  • Thank you received: 2322
10 years 3 months ago #168325

Hi,

If it's working fine but when you add the same item, why not simply add a check in order to get the cart content, check the product to add, and if it's the same just set $do to false.

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

  • Posts: 13
  • Thank you received: 2
10 years 2 months ago #169135

thanks, very good idea, that makes it work ALMOST all the time.

sometimes, and I have no idea when (no pattern recognizable), it just says "the cart is empty".

is there any other way to delete all items from the cart without calling resetCart()?


thanks once again!

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

  • Posts: 82864
  • Thank you received: 13372
  • MODERATOR
10 years 2 months ago #169144

Yes. You can delete the reference to the cart in the table hikashop_cart and hikashop_cart_product with SQL queries.

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

  • Posts: 13
  • Thank you received: 2
10 years 2 months ago #169485

just for others if you have a similar problem - I did it this way (it also changes the currency if certain products are added):

function onBeforeCartUpdate(&$cartClass,&$cart,$product_id,$quantity,$add,$type,$resetCartWhenUpdate,$force,&$do)
{
$app=&JFactory::getApplication();
$pointProducts=array(4,5,6,7);
if (in_array($product_id,$pointProducts)) {
$app->setUserState( 'com_hikashop.currency_id',1);
} else
{
$app->setUserState( 'com_hikashop.currency_id',182);
}
$full = $cartClass->loadFullCart(true,true,true);
$rows = $full->products;
$first=array_shift(array_values($rows));
if ($first->product_id==$product_id)
{
$do=false;
return false;
} else
{
$cartClass->resetCart(false);
$cart=$cartClass->initCart();
$do=true;
return true;
}
}

The following user(s) said Thank You: nicolas

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

Time to create page: 0.077 seconds
Powered by Kunena Forum