0

I work on a plugin and want to add an inline JS script once the place order button is hit on the checkout page (Woocommerce extension). In other words is there a way to add inline JS once wp_enqueue_scripts has already been fired?

6
  • You can't add script once the page has loaded, unless you read it in from an AJAX call I suppose: does your place order button make an AJAX request?
    – Rup
    Jun 11 '21 at 9:15
  • But I'm not sure exactly what you're trying to do. Is this just some action to take when the button is pressed? Can you not load the script with the page and bind it to the place order button on a click handler or submit handler? (Does the place order button submit a form, meaning it'll reload the page anyway?)
    – Rup
    Jun 11 '21 at 9:16
  • have you tried the inline JS functions? A quick search of the official developer docs site for the word inline shows a function perfect for this
    – Tom J Nowell
    Jun 11 '21 at 9:20
  • @TomJNowell I think that wont work if the wp_enqueue_scripts has already been fired? Not sure. I think if you're looking to add inline JS after the enqueuing has already been done, and your theme supports it, you could stick some JS in using the wp_footer hook. Not ideal, but will get the job done.
    – Paul G.
    Jun 11 '21 at 10:36
  • Inline JS functions don't work but I have just managed to create a custom a place order button and fired my JS on the click handler as @Rup suggested. Thank everyone for the help!
    – 81avas
    Jun 11 '21 at 11:01
1

i don't know with version you use, but if you wan't load stuff on the initialisation of js you can have it on file using js :

( function( wp ) { your stuff } )( window.wp );

if you really want inline use in php ; https://developer.wordpress.org/reference/functions/wp_add_inline_script/

wp_add_inline_script( 'name_of_the_script_Woocommerce', <script> your thing</script>, 'after_or_before' );

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.