JS confirm popup before add to cart

  • Posts: 2143
  • Thank you received: 747
4 years 7 months ago #317504

-- HikaShop version -- : 4.2.3
-- Joomla version -- : 3.9.16
-- PHP version -- : 7.3

Hi,

I need to have a Javascript "confirm()" popup after the click on the add to cart button, where the product gets added to the cart if "ok" is clicked; in case of "cancel", the popup should close and the product should not get added to the cart.

The popup is no problem. In product view overrides like show_tabular.php I've added

<script>
	function atc_confirm() { 
		if (confirm("Are you sure?") == false) {
			console.log("cancelled");
		}
	}
</script>
and in quantity.php I've added the function to the onclick attribute, so it reads
onclick="atc_confirm(); if(window.hikashop.addToCart) { return window.hikashop.addToCart(this); }"

I've tried adding "return;" and else after the console.log but, if anything, it only leads to the checkout instead staying on the current page. Whatever I do, the product gets still added to the cart on "cancel".

What to do to prevent the product from being added to the cart on "cancel"? Admittedly I'm not good enough with JS to figure it out myself, sorry.

Thanks for a good hint!


Need help with customisations of layouts, style or other site development? PM me!
(Don't forget to turn on "E-mail notification of new messages" )

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

  • Posts: 82867
  • Thank you received: 13373
  • MODERATOR
4 years 7 months ago #317521

Hi,

You should change your JS code to:

<script>
	function atc_confirm(el) { 
		if (confirm("Are you sure?") == false) {
			return false;
		}
		return window.hikashop.addToCart(el);
	}
</script>
And then:
onclick="return atc_confirm(this);"

The following user(s) said Thank You: lousyfool

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

  • Posts: 2143
  • Thank you received: 747
4 years 7 months ago #317572

Excellent -- works great!

Thank you!


Need help with customisations of layouts, style or other site development? PM me!
(Don't forget to turn on "E-mail notification of new messages" )

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

Time to create page: 0.062 seconds
Powered by Kunena Forum