I am trying to write a plugin to allow using coupons that are stored on a separate system and can be accessed through a Web API. To that end, I want to use the filter woocommerce_get_shop_coupon_data.

The source code is

$coupon = apply_filters( 'woocommerce_get_shop_coupon_data', false, $this->code )

My code in the plugin constructor:

add_filter('woocommerce_get_shop_coupon_data', array($this, 'filter_woocommerce_get_shop_coupon_data'));

And the function:

public function filter_woocommerce_get_shop_coupon_data($arg1, $arg2) {
    return null;

And what I'm getting is bool(false) NULL, i.e. the second arg which is supposed to be the coupon code is null. I have no idea what is happening here.


You need to tell add_filter how many arguments it accepts (2) example:

add_filter('woocommerce_get_shop_coupon_data', array( $this, 'filter_woocommerce_get_shop_coupon_data' ), 10, 2 );

Be sure to enable WP_Debug - http://wpexplorer-themes.com/total/docs/enabling-wp-debug/ - on your server, because you should have gotten an error when trying to do this without the added argument.

Also I highly recommend using static classes when dealing with filters for easiser modification by developers using child themes or extra plugins. Example:

add_filter( 'woocommerce_get_shop_coupon_data', array( 'YOUR_CLASS_NAME', 'filter_woocommerce_get_shop_coupon_data' ), 10, 2 );

public static function filter_woocommerce_get_shop_coupon_data( $arg1, $arg2 ) {
    return null;

This way a developer can always use this if needed:

remove_filter( 'woocommerce_get_shop_coupon_data', array( 'YOUR_CLASS_NAME', 'filter_woocommerce_get_shop_coupon_data' ), 10, 2 );

When using $this WordPress assigns a dynamic name and it's harder to modify things if needed. Just a tip. Maybe not for this plugin bug for others ;)

| improve this answer | |
  • I highly recommend using static functions though for your class - this way it's easier for a developer to remove your code. See my edited response ;) – WPExplorer Mar 6 '17 at 3:28
  • Thank you!!! And the 10 is its priority, am I right? – lfk Mar 6 '17 at 3:46
  • Yes 10 is the priority ;) But you can use anything - if 10 isn't working use 20 maybe. If this is intended to override ANYTHING you can also use PHP_INT_MAX – WPExplorer Mar 6 '17 at 16:35

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.