Trigger after product is added to cart?

  • Posts: 1119
  • Thank you received: 114
5 years 5 months ago #308034

Hi,

Is there any js function after product is added succesfully to cart i could use?

I need to add some animated icon on add to cart button after product is added to cart. I see there is some kinda function triggered while adding is in progress which adds "add_in_progress" class to the button...

Would be great to add some class ( temporary for 300ms or 500ms ) to button like "add_success" after product is added to cart.

Can this be done as improvement by default else any hints would be welcome.

Thank you

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

  • Posts: 200
  • Thank you received: 75
5 years 5 months ago #308045

Hi,

yes this function already exists, and this is also what the cart notify plugin already does, temporarily showing a pop-up message when a product is (or isn't) successfully added to the cart. You can have a look at the file plugins/hikashop/cartnotify/media/notify.js to see how this works. If you have the plugin enabled, you could add your code to add a class to a button directly in the plugin, but you can also register the cart update event in view overrides wherever you like.

For example the cart module also has this on line 15 to 28 if you look at the file com_hikashop/views/product/tmpl/cart.php. Here line 25 is the actual javascript that happens after the cart is updated, in this case remove a class, but you can just as easily add a class to a button or anything else.

Hope that helps.

The following user(s) said Thank You: nicolas, kyratn

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

  • Posts: 1119
  • Thank you received: 114
5 years 5 months ago #308074

Hi,

Thanks for the hint GW.

I am not willing to modify core plugin code so have looked into cart.php as you suggested.

It was easy to understand how it works and i adjusted code bellow which works partially.

<script type="text/javascript">
window.Oby.registerAjax(<?php echo $events; ?>, function(params) {
	var o = window.Oby, el = document.getElementById('hikashop_cart_<?php echo $module_id; ?>'),
        btn = document.getElementById('btn-add-cart');
	if(!el) return;
	if(params && params.resp && (params.resp.ret === 0 || params.resp.module == <?php echo (int)$module_id; ?>)) return;
	if(params && params.type && params.type != '<?php echo $this->cart_type; ?>') return;
	o.addClass(el, "hikashop_checkout_loading");
	o.xRequest("<?php echo hikashop_completeLink('product&task=cart&module_id='.$module_id . '&module_type='.$this->cart_type.'&return_url='.urlencode(base64_encode(hikashop_currentURL('return_url'))), true, false, true); ?>", {update: el}, function(xhr){
		o.removeClass(el, "hikashop_checkout_loading");
                o.addClass(btn, "add_success");
	});
});
</script>

The issue is that "add_success" class is also added after product is removed from cart because the event is for update... I am not sure how to fire it only after product is added to cart not removed, updated?

One more hint would be welcome.

Thanks again

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

  • Posts: 82868
  • Thank you received: 13376
  • MODERATOR
5 years 5 months ago #308084

Hi,

As GW said, you should look at how it's done in the plugins/hikashop/cartnotify/media/notify.js file
As you see there, there is an extra check on params.notify so that it will stop if that parameter is not true.
You need to add a similar check:

if(params.notify) o.addClass(btn, "add_success");

The following user(s) said Thank You: kyratn

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

Time to create page: 0.069 seconds
Powered by Kunena Forum