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?
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.
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.
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 beshow_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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With