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 Answers
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 );
}

Leave a Reply

Your email address will not be published. Required fields are marked *