I've added a custom product type to my woocommerce site called "Gift Card", and enabled the "Inventory" product data tab using the following code:
// Register the product type
function register_product_type () {
class WC_Product_Gift_Card extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'gift_card';
parent::__construct( $product );
// add additional functions here
}
}
}
add_action( 'init', 'register_product_type' );
// Add the product type to the dropdown select
function add_product_type ( $type ) {
$type[ 'gift_card' ] = __( 'Gift Card' );
return $type;
}
add_filter( 'product_type_selector', 'add_product_type' );
// Show the "Inventory" tab and hide the "Shipping" tab
function product_data_tabs( $tabs ) {
global $post, $product_object;
if ( $product_object && $product_object->is_type( 'gift_card' ) ) {
$tabs['inventory']['class'][] = 'show_if_gift_card';
unset( $tabs['shipping'] );
}
return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'product_data_tabs', 10, 1 );
However, on the Inventory tab, the "Manage stock levels (quantity)" option isn't shown. I've tried enabling it with the following code but it throws an error:
function show_manage_stock_options( $panels ) {
global $product_object;
if ( $product_object && $product_object->is_type( 'gift_card' ) ) {
$inventory_panel = $panels['inventory'];
$inventory_panel['fields']['manage_stock']['class'][] = 'show_if_gift_card';
$inventory_panel['fields']['stock_quantity']['class'][] = 'show_if_gift_card';
$panels['inventory'] = $inventory_panel;
}
return $panels;
}
add_filter( 'woocommerce_product_data_panels', 'show_manage_stock_options', 10, 2 );
Where am I going wrong?
just handle it with javascript
function woo_add_gift_card() {
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_gift_card').show();
jQuery('#general_product_data .pricing').addClass('show_if_gift_card').show();
//for Inventory tab
jQuery('.inventory_options').addClass('show_if_gift_card').show();
jQuery('#inventory_product_data ._manage_stock_field').addClass('show_if_gift_card').show();
jQuery('#inventory_product_data ._sold_individually_field').parent().addClass('show_if_gift_card').show();
jQuery('#inventory_product_data ._sold_individually_field').addClass('show_if_gift_card').show();
});
</script>
<?php
}
add_action( 'admin_footer', 'woo_add_gift_card' );