rafsuntaskin
11/14/2016 - 4:22 PM

woocommerce custom price field

woocommerce custom price field

// 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() {

  global $woocommerce, $post;
  
  echo '<div class="options_group">';
  
 woocommerce_wp_text_input( 
	array( 
		'id'          => '_show_price_rt', 
		'label'       => __( 'Show Price / Unit Price', 'woocommerce' ), 
		'placeholder' => '44e/a'
		
	)
);
  
  echo '</div>';
	
}

function woo_add_custom_general_fields_save( $post_id ){
	
	// Text Field
	$woocommerce_text_field = $_POST['_show_price_rt'];
	if( !empty( $woocommerce_text_field ) )
		update_post_meta( $post_id, '_show_price_rt', esc_attr( $woocommerce_text_field ) );

	
}

add_filter('woocommerce_get_price_html', 'rt_custom_price', 10, 2);

function rt_custom_price( $price, $product ) {

 $cp = get_post_meta($product->id, '_show_price_rt', true);
  if( !empty($cp) ){
	return  get_woocommerce_currency_symbol().$cp;
  }
  //var_dump($product->id);
  return $price;
}