Clear Cart before adding a product

  • Posts: 191
  • Thank you received: 13
3 months 1 week ago #301358

I'm using the Donations/Custom Price plugin to pass orders from a custom component to the Hikashop cart which works beautifully 99% of the time. I would like to limit the number of items allowed in the cart to just 1 which I can do in the configuration, however when a customer decides to change their selection they are not understanding that they need to remove the first product before adding a replacement. Is it possible for me to force the cart to be emptied each time before the plugin adds a product?

Last edit: 3 months 1 week ago by rodfarrell. Reason: Correctec plugin name

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

  • Posts: 65077
  • Thank you received: 9449
  • MODERATOR
3 months 1 week ago #301374

Hi,

That plugin doesn't add the product to the cart.
It only changes the price of a product in the cart based on a custom item field value attached to that product.

The add to cart process is done, I suppose, by a redirect of your custom component to the add to cart URL of the product with the custom item field value, or with custom code in the custom component which directly adds the product to the cart and then redirects to the checkout. In both cases, the best is to add some code before all that in the custom component to clear the cart. It can be done easily with such code once HikaShop's helper is loaded:

		$cartClass = hikashop_get('class.cart');
		$cart_id = $cartClass->getCurrentCartId();
		if(!empty($cart_id))
			$cartClass->delete($cart_id);

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

  • Posts: 191
  • Thank you received: 13
3 months 1 week ago #301417

Thanks Nicolas,
yes we do a redirect to the cart with the product details in the url.(/index.php?option=com_hikashop&ctrl=product&task=updatecart&quantity=1&product_id=1&data[item][amount]=$invoiceAmount&....)
That code snippet looks to be exactly what I was hoping for. Will test it today.

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

  • Posts: 191
  • Thank you received: 13
3 months 1 week ago #301419

That code gives me an error "Call to undefined function hikashop_get()"
Do I have to call the Hikashop helper first?

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

  • Posts: 65077
  • Thank you received: 9449
  • MODERATOR
3 months 1 week ago #301425

Hi,

Yes, that's what I was saying in my previous message :)
You can do it like that:

if(!defined('DS'))
			define('DS', DIRECTORY_SEPARATOR);
		if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php'))
			return true;

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

Time to create page: 0.061 seconds
Powered by Kunena Forum