0

I want to run Javscript once a product has been added to cart (with the script including product info).

The only thing I have tried is hook_commerce_cart_product_add() with drupal_add_js() but this doesn't seem to work. Ideally I would like it work like drupal_set_message(), so the script displays on the next page view.

I also need to get this working with removing products from the cart.

EDIT: Well I've discovered that doing the following actually works, but seems dodgy as hell, and would like to find a better way:

drupal_set_message("<script>alert('TEST');</script>");

Any ideas would be welcomed!

2
  • use a click function on the add to cart button div?
    – No Sssweat
    Oct 14 '15 at 6:57
  • Are you using an AJAX add to cart module or are you using the standard add to cart functionality that refreshes the page?
    – nvahalik
    Oct 20 '15 at 2:01
0

You should better use Rules here and the related events. The hook you mention (hook_commerce_cart_product_add) is a rules event hook.

Commerce Cart: After adding/removing a product to the cart

Then on the action add with php the js (drupal_add_js()) but if you simply want to show a message there is an action to do this easily.

System: Show a message on the site

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.