Skip to content

Instantly share code, notes, and snippets.

@rafsuntaskin
Created November 14, 2016 16:22
Show Gist options
  • Save rafsuntaskin/8b7649f66201e8298a5d3de355a612f0 to your computer and use it in GitHub Desktop.
Save rafsuntaskin/8b7649f66201e8298a5d3de355a612f0 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment