How to enable price and inventory for a custom product type in WooCommerce

I have created a custom product type in my WooCommerce app

function register_variable_bulk_product_type() {

    class WC_Product_Variable_bulk extends WC_Product_Simple {

        public function __construct($product) {

            $this->product_type = 'variable_bulk';
            parent::__construct($product);
        }

    }

}

add_action('init', 'register_variable_bulk_product_type');

function add_variable_bulk_product($types) {

    $types['variable_bulk'] = __('Variable Bulk');

    return $types;
}

add_filter('product_type_selector', 'add_variable_bulk_product');

      

This shows the product type in the product data dropdown as shown below:

enter image description here

My problem

There is no way to add resources and prices in the new product. How do I enable these options?

+3


source to share


1 answer


You will need a little JS trick to display the Price and Inventory tab, i.e. you need to add a class show_if_{your_custom_product_type}

to your case will show_if_variable_bulk

.

Here's the working code:

function wh_variable_bulk_admin_custom_js() {

    if ('product' != get_post_type()) :
        return;
    endif;
    ?>
    <script type='text/javascript'>
        jQuery(document).ready(function () {
            //for Price tab
            jQuery('.product_data_tabs .general_tab').addClass('show_if_variable_bulk').show();
            jQuery('#general_product_data .pricing').addClass('show_if_variable_bulk').show();
            //for Inventory tab
            jQuery('.inventory_options').addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._manage_stock_field').addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._sold_individually_field').parent().addClass('show_if_variable_bulk').show();
            jQuery('#inventory_product_data ._sold_individually_field').addClass('show_if_variable_bulk').show();
        });
    </script>
    <?php

}

add_action('admin_footer', 'wh_variable_bulk_admin_custom_js');

      

The code goes into functions.php

your active child theme (or theme) file . Or also in any plugins PHP files.
The code has been tested and works.

This is how your shared tab will look like:



enter image description here

and this is how the inventory tab will look like

enter image description here

Hope this helps!

+3


source







All Articles