• Resolved theNathanBaker

    (@thenathanbaker)


    Hi all,

    I’m trying to add a simple onClick event to the “Add to cart” and “Place Order” buttons. However, it doesn’t seem to be working.

    Where I’m adding the code:
    Add to cart: woocommerce > templates > single-product > add-to-cart > simple.php <button element>

    Place Order: woocommerce > templates > checkout > payment.php <button element>

    When I edit the file, the code saves and shows fine in dev tools but no event is triggered.

    Any ideas?

    Thanks,

    Nathan

Viewing 4 replies - 1 through 4 (of 4 total)
  • See if you have a theme template override at:
    themes/your-theme-name/woocommerce/checkout/payment.php

    If you have a child theme, check that also.

    If you do have an override, that file will take precedence over the woocommerce version.

    Check simple.php similarly.

    Thread Starter theNathanBaker

    (@thenathanbaker)

    Good callout, but I’m using their included ‘storefront’ theme and can’t find any conflicts or overrides with the plugin template pages. (Unless I’m just completely missing something).

    Right, Storefront doesn’t have a woocommerce directory.

    I think its possible that the WooCommerce javascript is setting up event listeners on add-to-cart buttons. That way, the add-to-cart button can be made to do something without the need to write an onclick function into the markup. The handler will prevent your further event from firing.

    The WC add-to-cart handler will use ajax to actually add the item to the cart through a PHP call, and WooCommerce has action hooks which you can use to hook into that and do something server side if that’s your objective.

    If you want to do something client side and hook into the WooCommerce javascript, there does seem to be ways of doing that but its too deep for me.

    I don’t know how the WooCommerce javascript knows which are add-to-cart buttons when it sets up the event handlers. Presumably it does it by selecting elements by class name. If so, you might try having a custom class name for your add-to-cart buttons, but if the WooCommerce javascript no longer handles the add-to-cart task, you’ll need to do that inside your own click handler.

    Thread Starter theNathanBaker

    (@thenathanbaker)

    Definitely some great things listed here to investigate, I appreciate the help!

    What I’m trying to do is a very simple js function to do an event call for pixel tracking.

    I’ll look into the things mentioned above and hopefully find a work-around. (Will post back here if I find anything worth mentioning).

    Thanks for the help!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Adding onClick event to woo commerce buttons’ is closed to new replies.