Created
November 14, 2016 16:22
-
-
Save rafsuntaskin/8b7649f66201e8298a5d3de355a612f0 to your computer and use it in GitHub Desktop.
woocommerce custom price field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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