Clear Cart before adding a product

  • Posts: 256
  • Thank you received: 21
5 years 11 months 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: 5 years 11 months ago by rodfarrell. Reason: Correctec plugin name

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
5 years 11 months 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: 256
  • Thank you received: 21
5 years 11 months 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: 256
  • Thank you received: 21
5 years 11 months 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: 82863
  • Thank you received: 13372
  • MODERATOR
5 years 11 months 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.

  • Posts: 267
  • Thank you received: 5
2 years 11 months ago #337393

Hi Nicolas,
I have the same problem, I have the cart option so that only one product can be added.
When someone selects a product and then selects a different one, it does not change and the original product remains in the cart.
I see that you explain the solution in this query, but I don't know very well where to make the modification. Could you help me on this?
Thanks!

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
2 years 11 months ago #337396

Hi,

I doubt you have the same problem as the problem was linked to some customization.
Could you provide a link to the shop with the issue and instructions to reproduce your issue so that I can have a look at the situation and understand what's going on ?

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

  • Posts: 267
  • Thank you received: 5
2 years 11 months ago #337400

Hello Nicolas,

when you choose a product from this page ...

www.universidadpopular.es/actividades-cu...es/eventos-en-la-upl

.... the product is added correctly the first time. If you return to the page and choose another of the products in the cart, the first product remains. (Occurs with the products of the forum of spirituality).

Thanks!

Last edit: 2 years 11 months ago by dvddvd.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
2 years 11 months ago #337401

Hi,

So I checked your page. From what I understand, you've configured your shop, probably with a limit, so that the customer cannot add several products of that page in the same cart.
After I add a product to the cart on your website, I'm getting automatically redirected to the checkout, without any notification box.
Normally in HikaShop you have a blue notification box which tells you that the product was successuflly add to the cart, or a red one which tells you that there is a problem.
The fact that you don't get it probably indicates that you've disabled the HikaShop cart notification plugin via the joomla plugins manager.
That's unfortunate because this leads to the system not displaying the error message when you add the second product to the cart.
If I manually access the add to cart URL of the second product, I get this:
i.imgur.com/rSt86K4.png
which tells me about the limit you must have configured.
So I would recommend to enable back this plugin. If you want HikaShop to redirect to the checkout after the add to cart, you can do it with that plugin activated, you just need to configure it accordingly. It will have the benefit of displaying an error message when there is a problem instead of directing you to the checkout without any indication of what's going on.

And regarding the limitation I can recommend several solutions:
- one would be to do like rodfarrell and develop a custom plugin to clear the cart before the add to cart process. Possible but it requires a developer to work on that for you.
- another would be to remove the limit, and add it instead as a shipping/payment method maximum quantity restriction. That way, the user would be able to add several products to the cart without problem, but he would be blocked on the checkout with an error message that can be customized to tell them that they have to remove from the cart some of the products to proceed. And you can configure the checkout workflow to display the cart view at this place so that they can easily do that.
- another solution would be to leave the limit like this, enable to cart notification plugin to get the error message when adding a second product and stay on the current page, and configure the HikaShop cart module, via the joomla plugins manager to display on that products listing so that the user can easily remove the product already in the cart.

The following user(s) said Thank You: dvddvd

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

Time to create page: 0.084 seconds
Powered by Kunena Forum