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

耗尽温柔 提交于 2019-12-05 02:06:26

问题


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,

My problem is

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


回答1:


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:

and this is how inventory tab will look

Hope this helps!



来源:https://stackoverflow.com/questions/43109755/how-to-enable-price-and-inventory-for-custom-product-type-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!