Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I created a custom product type in my WooCommerce application

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 product type in product data dropdown like as follows,

enter image description here

My problem is

The new product don't have an option for adding inventory and prices, How can I enable these options?

like image 876
Shijin TR Avatar asked Mar 30 '17 06:03

Shijin TR


People also ask

How do I enable stock management in WooCommerce?

Go to: WooCommerce > Settings > Products > Inventory. Enable stock management – Inventory for physical products is auto-managed. You enter quantity, and WooCommerce subtracts items as sales are made, displaying: Stock, Out of Stock or On Backorder.

What is the default WooCommerce product type?

Simple Product This is the default type that WooCommerce sets each time you create a new product. Configuring a simple WooCommerce product is a straightforward process. On the first available tab, General, you get to set the price. WooCommerce allows you to add three pricing tiers to a simple product.


1 Answers

You need a small JS trick to show Price and Inventory tab, i.e. you need to add class show_if_{your_custom_product_type} in your case it will be show_if_variable_bulk.

Here is 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');

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin PHP files.
Code is tested and works.

This is how your general tab will look:

enter image description here

and this is how inventory tab will look

enter image description here

Hope this helps!

like image 71
Raunak Gupta Avatar answered Sep 21 '22 22:09

Raunak Gupta