0

in my functions.php I try to remove an action from a plugin class. See both codes below. the remove doesn't work, but when I comment out the add_action (line 39) in the plugin class, then it works. What am I doing wrong?

function remove_trusted_theme_for_wholesale_customers() {
remove_action('woocommerce_thankyou', 
array('WC_GZD_Trusted_Shops_Template_Hooks','template_thankyou'), 10);
};
add_action( 'template_redirect', 'remove_trusted_theme_for_wholesale_customers' );

Here is the plugin:

<?php

class WC_GZD_Trusted_Shops_Template_Hooks {

    protected static $_instance = null;

    public $base = null;

    public static function instance( $base ) {
        if ( is_null( self::$_instance ) )
            self::$_instance = new self( $base );
        return self::$_instance;
    }

    private function __construct( $base ) {

        $this->base = $base;

        // Template actions
        if ( $this->base->is_enabled() )
            add_action( 'after_setup_theme', array( $this, 'template_hooks' ), 13 );

        if ( $this->base->is_product_reviews_enabled() ) {
            add_filter( 'woocommerce_product_tabs', array( $this, 'remove_review_tab' ), 40, 1 );
        }

        if ( $this->base->is_product_sticker_enabled() ) {
            add_filter( 'woocommerce_product_tabs', array( $this, 'review_tab' ), 50, 1 );
        }

        if ( $this->base->is_product_widget_enabled() ) {
            add_filter( 'woocommerce_gzd_template_name', array( $this, 'set_product_widget_template' ), 50, 1 );
        }

    }

    public function template_hooks() {

        add_action( 'woocommerce_thankyou', array( $this, 'template_thankyou' ), 10, 1 );
        add_action( 'wp_footer', array( $this, 'template_trustbadge' ), PHP_INT_MAX );

    }

    public function set_product_widget_template( $template ) {

        if ( in_array( $template, array( 'single-product/rating.php' ) ) )
            $template = 'trusted-shops/product-widget.php';

        return $template;

    }

    public function remove_review_tab( $tabs ) {

        if ( isset( $tabs[ 'reviews' ] ) )
            unset( $tabs[ 'reviews' ] );
        return $tabs;

    }

    public function review_tab( $tabs ) {
        $tabs[ 'trusted_shops_reviews' ] = array(
            'title' => _x( 'Reviews', 'trusted-shops', 'woocommerce-germanized' ),
            'priority' => 30,
            'callback' => array( $this, 'template_product_sticker' ),
        );
        return $tabs;
    }

    public function template_product_sticker( $template ) {
        wc_get_template( 'trusted-shops/product-sticker.php' );
    }

    public function template_trustbadge() {
        wc_get_template( 'trusted-shops/trustbadge.php' );
    }

    public function template_thankyou( $order_id ) {
        wc_get_template( 'trusted-shops/thankyou.php', array(
            'order_id' => $order_id,
            'gtin_attribute' => $this->base->gtin_attribute,
            'brand_attribute' => $this->base->brand_attribute,
            'mpn_attribute' => $this->base->mpn_attribute,
        ) );
    }

}
3

What you're doing will only remove an action added in a static method of class.

From https://codex.wordpress.org/Function_Reference/remove_action

If an action has been added from within a class, for example by a plugin, removing it will require accessing the class through a variable that holds the class instance.

add_action( 'wp_head', 'remove_my_class_action' );
function remove_my_class_action(){
  global $my_class;
  remove_action( 'wp_footer', array( $my_class, 'class_function_being_removed' ) );
}
  • Thanks, so it is function remove_trusted_theme_for_wholesale_customers() { global $my_class; remove_action('woocommerce_thankyou', array($my_class,'WC_GZD_Trusted_Shops_Template_Hooks::template_thankyou'), 10); }; add_action( 'init', 'remove_trusted_theme_for_wholesale_customers' ); ? – radscheit Mar 8 '18 at 16:13
  • 1
    you need an instance of the class WC_GZD_Trusted_Shops_Template_Hooks. You should be able to get an instance by using $myInstance = WC_GZD_Trusted_Shops_Template_Hooks::instance() and then do remove_action('woocommerce_thankyou', array($myInstance, 'template_thankyou', 10); It's also possible that may need to prioritize the removal of the action to a hook that occurs after the action is added. – josephmcm Mar 8 '18 at 16:23

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.