Skip to content

Instantly share code, notes, and snippets.

@sutandang
Created June 6, 2017 15:36
Show Gist options
  • Save sutandang/acbe59af46ac7dd7362dd4592c635438 to your computer and use it in GitHub Desktop.
Save sutandang/acbe59af46ac7dd7362dd4592c635438 to your computer and use it in GitHub Desktop.
custom woocommerce product type in admin woocommerce / product tabs
<?php
add_filter( 'woocommerce_product_data_tabs', 'add_my_custom_product_data_tab' , 99 , 1 );
function add_my_custom_product_data_tab( $product_data_tabs ) {
/*
to remove this product type unset all product type
unset($product_data_tabs['general']);
unset($product_data_tabs['inventory']);
unset($product_data_tabs['shipping']);
unset($product_data_tabs['linked_product']);
unset($product_data_tabs['attribute']);
unset($product_data_tabs['variations']);
unset($product_data_tabs['advanced']);*/
/*add new array to create product type
to create hide show field you can use condition
'class' => array( 'hide_if_variable' ) to hide form if product type variable
'class' => array( 'hide_if_variable' ) to hide form if product type simple
and etc.
*/
$product_data_tabs['company_name'] = array(
'label' => __( 'company_name', 'my_text_domain' ),
'target' => 'my_custom_product_data_company_name',
);
$product_data_tabs['fix_price'] = array(
'label' => __( 'Price', 'my_text_domain' ),
'class' => array( 'hide_if_variable' ),
'target' => 'my_custom_product_data_simple',
);
$product_data_tabs['variable_price'] = array(
'label' => __( 'Budget', 'my_text_domain' ),
'class' => array( 'hide_if_simple' ),
'target' => 'my_custom_product_data_variable',
);
return $product_data_tabs;
}
add_filter('product_type_options',function(){return array();},99,1);
add_action( 'woocommerce_product_data_panels', 'add_my_custom_product_data_fields' );
function add_my_custom_product_data_fields() {
/*create the form */
global $woocommerce, $post;
?>
<!-- id below must match target registered in above add_my_custom_product_data_tab function -->
<div id="my_custom_product_data_variable" class="panel woocommerce_options_panel">
<?php
woocommerce_wp_text_input( array(
'id' => '_price_from',
'label' => __( 'From', 'woocommerce' ),
'wrapper_class' => 'show_if_variable hide_if_simple',
'desc_tip' => true,
'description' => __( 'Fixed Price', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any'
),
'data_type' => 'stock'
) );
woocommerce_wp_text_input( array(
'id' => '_price_to',
'label' => __( 'To', 'woocommerce' ),
'wrapper_class' => 'show_if_variable hide_if_simple',
'desc_tip' => true,
'description' => __( 'Fixed Price', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any'
),
'data_type' => 'stock'
) );
?>
</div>
<div id="my_custom_product_data_simple" class="panel woocommerce_options_panel">
<?php
woocommerce_wp_text_input( array(
'id' => '_price',
'label' => __( 'Price', 'woocommerce' ),
'wrapper_class' => 'show_if_simple hide_if_variable',
'desc_tip' => true,
'description' => __( 'Fixed Price', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any'
),
'data_type' => 'stock'
) );
?>
</div>
<?php
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_process_product_meta_fields_save' ,100);
function woocommerce_process_product_meta_fields_save( $post_id ){
/*to save data after submit */
$_price_from = isset($_POST['_price_from']) ? htmlspecialchars($_POST['_price_from']) : '';
$_price_to = isset($_POST['_price_to']) ? htmlspecialchars($_POST['_price_to']) : '';
update_post_meta($post_id,'_price_from',$_price_from);
update_post_meta($post_id,'_price_to',$_price_to);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment