1

Ok I'm struggling here - I feel this should be straightforward but I'm just not able to get it to work.

I need to remove a filter added by Woocommerce Subscriptions. The filter is part of the WC_Subscriptions_Switcher class found in class-wc-subscriptions-switcher.php and is added as below

class WC_Subscriptions_Switcher {

    /**
     * Bootstraps the class and hooks required actions & filters.
     *
     * @since 1.4
     */
    public static function init() {
         ...

         // Display/indicate whether a cart switch item is a upgrade/downgrade/crossgrade
         add_filter( 'woocommerce_cart_item_subtotal', __CLASS__ . '::add_cart_item_switch_direction', 10, 3 );
         ...

The function adds text to the cart (either (Upgrade), (Downgrade) or (Crossgrade) which I don't want/need to be displayed in my case.

I have tried simply using the remove filter function as below from the Wordpress Codex however the text in question is still displayed regardless.

 global $WC_Subscriptions_Switcher;
   remove_filter( 'woocommerce_cart_item_subtotal', array($WC_Subscriptions_Switcher, 'add_cart_item_switch_direction') );

I have also tried everything (I think) I found at the below answer and also simply tried the below from the remove_action or remove_filter with external classes?

How do I remove this filter so that it's not fired in the first place? I know I could override the text using gettext but I think it's cleaner to not have it run in the first place (and even using gettext the html tags added by the filter will remain).

Any help appreciated.

2

You did not write where you put the filter removal code, but I suspect you tried to delete it before it was added. Another important point, you have not given the priority parameter in remove_filter().

Quote from the documentation:

remove_filter( $tag, $function_to_remove, $priority );

Important: To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.

Try removing filter when the plugins are already loaded and enter both required parameters - function name and priority.

add_action( 'plugins_loaded', 'se334421_remove_plugin_filter' );
function se334421_remove_plugin_filter() {
    remove_filter( 'woocommerce_cart_item_subtotal', 'WC_Subscriptions_Switcher::add_cart_item_switch_direction', 10 );
}
  • Thanks. I had tried wp_footer and plugins_loaded but possibly plugins_loaded with the right priority. All working now - I knew it would be something simple! Thanks for your help. – Josh Apr 15 at 12:43
0

Set priority with your action call like as follows:

remove_filter('woocommerce_cart_item_subtotal', array($WC_Subscriptions_Switcher, 'add_cart_item_switch_direction') , 99);

You can use numbers in between 0-99 for setting priority.

  • 1
    Thanks. I thought I had done that but maybe I didn't set it high enough or was one of the combinations I didn't try. All working now. – Josh Apr 15 at 12:42

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.