How to hide/remove Restrictions Tab in WooCommerce Product Data page? Please help!

mrPoor

New member
Jul 29, 2020
7
1
3
Hi,
I'm using WooCommerce and WooCommerce Catalog Visibility Options plugins.
I want to remove some tabs in product data box, such as General, Inventory, Shipping and Restrictions tabs. I have removed the first three tabs, except for the Restrictions, by adding the code below in functions.php:
PHP:
add_filter( 'woocommerce_product_data_tabs', 'woo_remove_product_tabs', 10, 1 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['general'] );
unset( $tabs['inventory'] );
unset( $tabs['shipping'] );
unset( $tabs['restrictions'] );
return $tabs;
}
From researching, I know that the Restrictions tab is added by WooCommerce Catalog Visibility Options plugin. I dig round into the source code and I found the way how the Restrictions was added:
PHP:
public function __construct() {
add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'add_tab' ) );
}

public function add_tab() {
?>
<li class="wc_catalog_restrictions_tab wc_catalog_restrictions">
<a href="#wc_catalog_restrictions"><span><?php _e( 'Restrictions', 'wc_catalog_restrictions' ); ?></span></a>
</li><?php
}

I also tried this:
PHP:
add_filter( 'woocommerce_product_write_panel_tabs', 'remove_restrictions_tab' );
function remove_restrictions_tab( $tabs ) {
unset( $tabs['restrictions'] );
return $tabs;
}

But it didn't work at all. I'm kind new to php, can anyone help me to remove the restrictions tab properly?
 
Last edited:

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
Haven't tested this myself, but have you tried something like this:
PHP:
function remove_plugin_tab() {
    remove_action( 'woocommerce_product_write_panel_tabs', 'WC_Catalog_Restrictions_Product_Admin::add_tab', 10 );
}

add_action( 'plugins_loaded', 'remove_plugin_tab' );
 

mrPoor

New member
Jul 29, 2020
7
1
3
Haven't tested this myself, but have you tried something like this:
PHP:
function remove_plugin_tab() {
    remove_action( 'woocommerce_product_write_panel_tabs', 'WC_Catalog_Restrictions_Product_Admin::add_tab', 10 );
}

add_action( 'plugins_loaded', 'remove_plugin_tab' );
Thank you for your response. I tried your suggested code, but it didn't work. However, can you explain this part for me please?

PHP:
'WC_Catalog_Restrictions_Product_Admin::add_tab'
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
The function add_tab is part of the Class "WC_Catalog_Restrictions_Product_Admin", so I was hoping this would work after the plugin was loaded. Apparently not.
One more suggestion:
PHP:
global $WC_Catalog_Restrictions_Product_Admin;
remove_action('woocommerce_product_write_panel_tabs', array($WC_Catalog_Restrictions_Product_Admin, 'add_tab') , 99);
If that also doesn't work, I'm out of ideas, sorry.
 

mrPoor

New member
Jul 29, 2020
7
1
3
The function add_tab is part of the Class "WC_Catalog_Restrictions_Product_Admin", so I was hoping this would work after the plugin was loaded. Apparently not.
One more suggestion:
PHP:
global $WC_Catalog_Restrictions_Product_Admin;
remove_action('woocommerce_product_write_panel_tabs', array($WC_Catalog_Restrictions_Product_Admin, 'add_tab') , 99);
If that also doesn't work, I'm out of ideas, sorry.

Unfortunately, it doesn't work :(. However, there was a friend who answer me on stackoverflow as below:

In meta-boxes/views/html-product-data-panel.php on line 41-48 we find
<ul class="product_data_tabs wc-tabs">
<?php foreach ( self::get_product_data_tabs() as $key => $tab ) : ?>
<li class="<?php echo esc_attr( $key ); ?>_options <?php echo esc_attr( $key ); ?>_tab <?php echo esc_attr( isset( $tab['class'] ) ? implode( ' ', (array) $tab['class'] ) : '' ); ?>">
<a href="#<?php echo esc_attr( $tab['target'] ); ?>"><span><?php echo esc_html( $tab['label'] ); ?></span></a>
</li>
<?php endforeach; ?>
<?php do_action( 'woocommerce_product_write_panel_tabs' ); ?>
</ul>
self::get_product_data_tabs() refers to meta-boxes/class-wc-meta-box-product-data.php line 82 which then contains woocommerce_product_data_tabs
/**
* Return array of tabs to show.
*
* @return array
*/
private static function get_product_data_tabs() {
$tabs = apply_filters(
'woocommerce_product_data_tabs',
array(
...
So woocommerce_product_write_panel_tabs runs after the foreach loop
To remove an action you need to pass the same callable that was used to add the action in the first place. Note the use of $this
add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'add_tab' ) );
So when you remove the action you need to pass the same instance the same way:
remove_action( 'woocommerce_product_write_panel_tabs', array( $that, 'add_tab' ) );
Where $that is a variable containing the same instance of the class. To do this you need to find that variable. This depends on how the plugin was originally built.

But I just can't understand the explanation in that post. Can you please be so kind to take a look and if whether it make any sense to you?

Thank you very much!
 
Last edited:

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
Finally found the solution, thanks to davelavoie:
PHP:
if ( ! function_exists( 'remove_class_hook' ) ) {
    function remove_class_hook( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
        global $wp_filter;
        $is_hook_removed = false;
        if ( ! empty( $wp_filter[ $tag ]->callbacks[ $priority ] ) ) {
            $methods     = wp_list_pluck( $wp_filter[ $tag ]->callbacks[ $priority ], 'function' );
            $found_hooks = ! empty( $methods ) ? wp_list_filter( $methods, array( 1 => $method_name ) ) : array();
            foreach( $found_hooks as $hook_key => $hook ) {
                if ( ! empty( $hook[0] ) && is_object( $hook[0] ) && get_class( $hook[0] ) === $class_name ) {
                    $wp_filter[ $tag ]->remove_filter( $tag, $hook, $priority );
                    $is_hook_removed = true;
                }
            }
        }
        return $is_hook_removed;
    }
}

add_action( 'init', function() {
    remove_class_hook( 'woocommerce_product_write_panel_tabs', 'WC_Catalog_Restrictions_Product_Admin', 'add_tab' );
});

The method above allows to remove filters/actions with a callback to a class you don't have access to.

Personally, I would just comment that action line in the plug-in and make a note to do it again, when the plug-in is updated.
 
  • Love
Reactions: mrPoor

mrPoor

New member
Jul 29, 2020
7
1
3
Finally found the solution, thanks to davelavoie:
PHP:
if ( ! function_exists( 'remove_class_hook' ) ) {
    function remove_class_hook( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
        global $wp_filter;
        $is_hook_removed = false;
        if ( ! empty( $wp_filter[ $tag ]->callbacks[ $priority ] ) ) {
            $methods     = wp_list_pluck( $wp_filter[ $tag ]->callbacks[ $priority ], 'function' );
            $found_hooks = ! empty( $methods ) ? wp_list_filter( $methods, array( 1 => $method_name ) ) : array();
            foreach( $found_hooks as $hook_key => $hook ) {
                if ( ! empty( $hook[0] ) && is_object( $hook[0] ) && get_class( $hook[0] ) === $class_name ) {
                    $wp_filter[ $tag ]->remove_filter( $tag, $hook, $priority );
                    $is_hook_removed = true;
                }
            }
        }
        return $is_hook_removed;
    }
}

add_action( 'init', function() {
    remove_class_hook( 'woocommerce_product_write_panel_tabs', 'WC_Catalog_Restrictions_Product_Admin', 'add_tab' );
});

The method above allows to remove filters/actions with a callback to a class you don't have access to.

Personally, I would just comment that action line in the plug-in and make a note to do it again, when the plug-in is updated.
Wow, it really works. Thank you very many!!! :D:D:D
 

JeffSimons

New member
Aug 18, 2020
1
0
1
I'm still new to Woo Commerce and there is still a lot that I need to learn from the platform. I plan on making a fan website https://akinator.io/ and I also would want to put a merch shop in there for fans. This is the reason why I considered Woo Commerce for the platform and I'm hoping that it will be stable enough.
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu