polupraneeth of Stackadroit
12/7/2016 - 8:08 AM

for creating custom metabox in woocoomerce

for creating custom metabox in woocoomerce

function woo_add_custom_general_fields() {

  global $woocommerce, $post;
  
  echo '<div class="options_group">';
  
  woocommerce_wp_text_input( 
	array( 
		'id'          => '_text_departure', 
		'label'       => __( 'Departure', 'woocommerce' ), 
		'placeholder' => 'dd/mm/yy',
		'desc_tip'    => 'true',
		'description' => __( 'Enter the Departure Time here.', 'woocommerce' ) 
	)
	);
	
	woocommerce_wp_text_input( 
	array( 
		'id'          => '_text_return', 
		'label'       => __( 'Return', 'woocommerce' ), 
		'placeholder' => 'dd/mm/yy',
		'desc_tip'    => 'true',
		'description' => __( 'Enter the Return Time here.', 'woocommerce' ) 
	)
	);
	
	woocommerce_wp_text_input( 
	array( 
		'id'          => '_text_ref', 
		'label'       => __( 'Ref', 'woocommerce' ), 
		'placeholder' => 'Group',
		'desc_tip'    => 'true'
		 
	)
	);
	
	woocommerce_wp_text_input( 
	array( 
		'id'          => '_text_nb', 
		'label'       => __( 'Nb', 'woocommerce' ), 
		'placeholder' => 'write here....',
		'desc_tip'    => 'true'
		
	)
	);
	// Select
woocommerce_wp_select( 
array( 
	'id'      => '_select', 
	'label'   => __( 'pacakge', 'woocommerce' ), 
	'options' => array(
		'one'   => __( 'Option 1', 'woocommerce' ),
		'two'   => __( 'Option 2', 'woocommerce' ),
		'three' => __( 'Option 3', 'woocommerce' )
		)
	)
);
  
  echo '</div>';
	
}
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields_save( $post_id ){
	
	// Text Field
	$woocommerce_text_field = $_POST['_text_departure'];
	if( !empty( $woocommerce_text_field ) )
		update_post_meta( $post_id, '_text_departure', esc_attr( $woocommerce_text_field ) );
		
	// Text Field
	$woocommerce_text_return = $_POST['_text_return'];
	if( !empty( $woocommerce_text_return) )
		update_post_meta( $post_id, '_text_return', esc_attr( $woocommerce_text_return) );	
	
	// Text Field
	$woocommerce_text_ref = $_POST['_text_ref'];
	if( !empty( $woocommerce_text_ref ) )
		update_post_meta( $post_id, '_text_ref', esc_attr( $woocommerce_text_ref) );	
		
	// Text Field
	$woocommerce_text_nb = $_POST['_text_nb'];
	if( !empty( $woocommerce_text_nb) )
		update_post_meta( $post_id, '_text_nb', esc_attr( $woocommerce_text_nb) );	
		
	// Select
	$woocommerce_select = $_POST['_select'];
	if( !empty( $woocommerce_select ) )
		update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );	
}
http://www.remicorson.com/mastering-woocommerce-products-custom-fields/