I was trying to add one more callback function to the WordPress AJAX action woocommerce_apply_coupon.

This action is defined in the WooCommerce plugin, I want to add my own callback function to this action from my plugin file.

What I have tried:

add_action( 'wp_ajax_nopriv_woocommerce_apply_coupon','darn',999);
add_action( 'wp_ajax_woocommerce_apply_coupon', 'darn',999);

function darn(){

Doing this in my functions.php is not even showing any error, like I can't see any effect of this code.

I want to know if this is even possible to achieve . Thank you.

  • I may have misunderstood your question, but an ajax call is made from a JS script, so I suppose to interact with woocommerce_ajax_apply_coupon you should make the call from a JS script – iEmanuele Aug 22 '13 at 10:22
  • yes you misunderstood completely – codepixlabs Aug 22 '13 at 10:28
  • Have you read this and/or this? – iEmanuele Aug 22 '13 at 10:37
  • yap none of them have mentioned anything related to multiple callbacks for a single action – codepixlabs Aug 22 '13 at 10:46
  • its okey if this title actually will help others looking for something similar – codepixlabs Aug 22 '13 at 11:29

Yes. It is possible and you're on the right track! :)

AJAX calls happen "behind the scenes" and no screen output is generated during the process no matter what you echo or print_r - that's why you don't see anything returned to you - instead that server response is captured by javascript and acted upon there.

That gives you two basic options for debugging your AJAX actions:

1) You can use error_log() in your AJAX actions to write the output to WP_DEBUG_LOG file and check the results there:

function darn(){
     error_log( print_r($_POST, 1) );

For more details see Codex: Debugging_in_WordPress - WP_DEBUG_LOG

Pro-tip #1: Fire up a terminal console and use tail -f debug.log for a live stream of debugging awesomness ;)

Pro-tip #2: Use tail -f debug.log | grep -v 'bad-plugin' to filter out too much debugging awesomness from plugin authors who don't use WP_DEBUG_LOG ;P

2) Use javascript's own console.log() to check the response right in your browser:

Send a response from the AJAX action...

function darn(){
    echo 'darn!'

... and receive it with JS:

$.post(ajaxurl, data, function(response) {
    // Response will be 'darn!'
    if( typeof(console) == 'object' ) console.log( response );

Pro-tip #3: Instead of simply echoing the response like we did above you can use wp_send_json() for generating a JSON formatted response or WP_Ajax_Response class for a XML. Handy for more complex responses and/or error handling!

Pro-tip #4: Let me know in comments if anything remains unclear :D

Welcome and have a great time on WPSE!

| improve this answer | |
  • thankyou this was very informative ! time to try it out :D – codepixlabs Aug 22 '13 at 10:48
  • 1
    +1 for wp_send_json() I didn't know it and still output json using php header. However advantage of old method is possibility to add cache-related headers. – gmazzap Aug 24 '13 at 13:23

If some other handler code is hooked first and it calls exit() too, then the script ends, so your code never runs.

Check the other handler.

| improve this answer | |

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.