admin管理员组

文章数量:1122846

I would like to override a WP (WooCommerce AJAX function) using add_filter.

The original code is set in a WC OOP class via something like:

add_action( 'wp_ajax_wc_function', array( $this, 'wc_do_stuff' ) );
add_action( 'wp_ajax_nopriv_wc_function', array( $this, 'wc_do_stuff' ) );

And wc_do_stuff is a public function.

My question(s):

  • What callback would I need to target?
  • Is it possible to use add_filter to override an AJAX function; if so, how would I do so?

I've tried (i.e. override 'wc_do_stuff' with 'my_override_do_stuff' ...within my custom class):

// Tried this, doesn't work
add_filter( 'wc_do_stuff', array( $this, 'my_override_do_stuff' ), 20 );

// And this, also doesn't work
add_filter( 'wc_do_stuff', array('WC_Parent_Class', 'my_override_do_stuff'), 20);

Thanks in advance!

I would like to override a WP (WooCommerce AJAX function) using add_filter.

The original code is set in a WC OOP class via something like:

add_action( 'wp_ajax_wc_function', array( $this, 'wc_do_stuff' ) );
add_action( 'wp_ajax_nopriv_wc_function', array( $this, 'wc_do_stuff' ) );

And wc_do_stuff is a public function.

My question(s):

  • What callback would I need to target?
  • Is it possible to use add_filter to override an AJAX function; if so, how would I do so?

I've tried (i.e. override 'wc_do_stuff' with 'my_override_do_stuff' ...within my custom class):

// Tried this, doesn't work
add_filter( 'wc_do_stuff', array( $this, 'my_override_do_stuff' ), 20 );

// And this, also doesn't work
add_filter( 'wc_do_stuff', array('WC_Parent_Class', 'my_override_do_stuff'), 20);

Thanks in advance!

Share Improve this question edited Jul 31, 2017 at 22:34 Ryan Dorn asked Jul 31, 2017 at 18:39 Ryan DornRyan Dorn 3499 silver badges23 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Ok, so I don't know if this is the best way, but it's working for now.

Within my OOP class (I have a custom plugin to override a WC plugin):

    remove_action( 'wp_ajax_wc_function', array( WC_Parent_Class, 'wc_do_stuff' ) );
    remove_action( 'wp_ajax_nopriv_wc_function', array( WC_Parent_Class, 'wc_do_stuff' ) );
    add_action( 'wp_ajax_wc_function', array( __CLASS__, 'my_override_do_stuff' ) );
    add_action( 'wp_ajax_nopriv_wc_function', array( __CLASS__, 'my_override_do_stuff' ) 

Hope this helps someone and I'd welcome/appreciate if anyone has a better method. :)

Just a little something (I can't comment OP's answer as I don't have enough rep right now).

If you want to override a function of a Class in your functions.php without using a class yourself, just do :

remove_action( 'wp_ajax_wc_function', array( WC_Parent_Class, 'wc_function' ) );
add_action( 'wp_ajax_wc_function', 'your_new_function' ); // no need to array

本文标签: filtersHow To Override A WooCommerce AJAX Function