wordpress add multiple callback function to single ajax action from different plugin

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 .

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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 !

  2. 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
    }