2

I was trying to add one more callback function to wordpress ajax action

woocommerce_apply_coupon

this wp_ajax action is defined in woocommerce plugin,I want to add my own callback function on 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(){
         print_r($_REQUEST);
         exit;
    }

Doing this in my functions.php is not even showing any error, like i cant see any effect of this code.

want to know if this is even possible to achieve . Thankyou.

2
  • How do you enable Ajaxified coupons? I cannot seem to find the option. Is it made available by the theme? Commented Aug 22, 2013 at 12:36
  • it has nothing to do with theme, coupons are applied via ajax on checkout page. have a look Commented Aug 22, 2013 at 12:39

2 Answers 2

2

well i would like to answer my own question since nobody seems to answer it .

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

function darn(){
         print_r($_REQUEST);
         exit;
    }

by setting priority to one (giving my callback function the top priority) it actually worked !

Sign up to request clarification or add additional context in comments.

Comments

1

This is untested, but I'd try to solve it removing the original actions after all plugins are loaded, and then adding your substitute:

add_action( 'plugins_loaded', 'b5f_new_ajax_coupon', 15 );

function b5f_new_ajax_coupon() 
{
    remove_action( 'wp_ajax_woocommerce_apply_coupon', 'woocommerce_ajax_apply_coupon' );
    remove_action( 'wp_ajax_nopriv_woocommerce_apply_coupon', 'woocommerce_ajax_apply_coupon' );
    add_action( 'wp_ajax_woocommerce_apply_coupon', 'b5f_ajax_apply_coupon' );
    add_action( 'wp_ajax_nopriv_woocommerce_apply_coupon', 'b5f_ajax_apply_coupon' );
}   

function b5f_ajax_apply_coupon()
{
    // COPY THE ORIGINAL /woocommerce/woocommerce-ajax.php#L57
    // AND ADAPT TO YOUR NEEDS
}

1 Comment

i have resolved this issue, thankyou for your help though :) it was priority which was bugging me !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.