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

    }

}

2 Answers
2

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.

Leave a Reply

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