1

I'm trying to set up a woocommerce custom product type so that its as minimal as possible - no options I don't specifically designate.

Still, despite the declaration below, the custom product meta box still shows "shipping" shipping options.

How do I get rid of the shipping options? And on that note, any other that I may wish to remove.

add_action('plugins_loaded', 'ss_create_custom_product_type');

function ss_create_custom_product_type() {

    // declare the product class
    class WC_Product_Wdm extends WC_Product {    
        public function __construct($product) {
            $this->product_type = 'ss_stock_image';


            $this->virtual = 'yes';
            $this->downloadable = 'yes';
            $this->manage_stock = 'no';   

            parent::__construct($product);
            // add additional functions here            
        }     
    }        
}

enter image description here

UPDATE:

I've updated the class per suggestion. Yet I wonder why the code is not getting fired.

add_action('plugins_loaded', 'ss_create_custom_product_type');

function ss_create_custom_product_type() {

    // declare the product class
    class WC_Product_Wdm extends WC_Product {

        public function __construct($product) {
            $this->product_type = 'ss_stock_image';

            add_filter('woocommerce_product_data_tabs', array($this, 'remove_tabs'), 10, 1);

            parent::__construct($product);
        }

        public function remove_tabs($tabs) {

            /**
             * The available tab array keys are:
             * 
             * general
             * inventory
             * shipping
             * linked_product
             * attribute
             * variations
             * advanced
             */
            unset($tabs['shipping']);
            return $tabs;
        }

    }

}
4
  • Are you positively sure that overriding those class properties then corresponds to the actual tabs being hidden in the UI and have you isolated the code that is responsible for printing those tabs in the UI in order to confirm?
    – Adam
    Mar 14 '15 at 8:36
  • Good question. After looking at the js files, I concluded that the tabs must be assigned based on the class's setup. But I could be wrong. JS files did not show anything obvious other than generic hide/show functions that apply systematically to custom product types.
    – 1Up
    Mar 14 '15 at 9:28
  • This question requires knowledge about third party plugin, so it is off-topic here.
    – cybmeta
    Mar 14 '15 at 10:31
  • @1Up You are not instantiating your WC_Product_Wdm class, for example, you need to add: new WC_Product_Wdm() after your class declaration in order for your class constructor to be called.
    – Adam
    Mar 14 '15 at 10:49
8

Looking further into the WooCommerce source, they fortunately, provide a filter named woocommerce_product_data_tabs which will allow you to conditional unset tabs.

I've provided an example below:

add_filter('woocommerce_product_data_tabs', function($tabs) {

    /**
     * The available tab array keys are:
     * 
     * general
     * inventory
     * shipping
     * linked_product
     * attribute
     * variations
     * advanced
     */

    unset($tabs['shipping']);
    return $tabs;

}, 10, 1);

You may also filter the product type drop down menu using the product_type_selector filter and also the product type options checkboxes using the product_type_options filter.

Example:

add_filter('product_type_selector', function($product_type) {

    /**
     * The available product type selection array keys are:
     * 
     * simple
     * grouped
     * external
     * variable
     */

    unset($product_type['variable']); //as an example...
    return $product_type;

}, 10, 1);

Example:

add_filter('product_type_options', function($product_options) {

    /**
     * The available product type options array keys are:
     * 
     * virtual
     * downloadable
     */

    unset($product_options['downloadable']); //as an example...
    return $product_options;

}, 10, 1);

For your reference I found these filters by first looking at the add_meta_boxes method contained within the WC_Admin_Meta_Boxes class within includes/admin/class-wc-admin-meta-boxes.php;

From there you will note that they add a meta box, whose id is, woocommerce-product-data and the callback of which is WC_Meta_Box_Product_Data::output found within includes/admin/meta-boxes/class-wc-meta-box-product-data.php.

It is within this output method that the filters woocommerce_product_data_tabs, product_type_selector and product_type_options exist.

4
  • Thank you very much for that. It works outside as a function, but I can't get it working within the class custom product class itself. But this is sufficient to at least get me started, so thank you very much.
    – 1Up
    Mar 14 '15 at 10:07
  • @1Up In your example code, you have a class which extends WC_Product wrapped in a function in which is called on the back of the plugins_loaded hook, but are you actually instantiating, anywhere, your WC_Product_Wdm class? Because if you are not, then code within that class is never getting called.
    – Adam
    Mar 14 '15 at 10:10
  • I've updated the question. This should work no? After all, if it is creating the custom product type, it should zip right through the add_filter function as well, no? Why is this backfiring? Your right - I placed a die() in the function just to see if it was being called and it is not.
    – 1Up
    Mar 14 '15 at 10:16
  • I found a nice work-around. See below answer. Too hackish? Is there a more elegant solution? In any case, seems sufficient -- thank you for your help.
    – 1Up
    Mar 14 '15 at 10:24
0

Actually @userabuser has the more complete answer, but to overcome this problem I was able to simply add a "hide" class this way:

add_filter('woocommerce_product_data_tabs', function($tabs) {

    array_push($tabs['shipping']['class'], 'hide_if_ss_stock_image');
    return $tabs;

}, 10, 1);

Notice the hide_if_ss_stock_image class that is added.

Not the answer you're looking for? Browse other questions tagged or ask your own question.