Not sure if you need to hook within the single product page, you can remove that part of the code if needs be.
code goes in functions.php of your child theme or a code snippets plugin.
/**
* Function to render UI in Admin Product add/edit page
*/
function show_custom_price_admin() {
global $thepostid;
woocommerce_wp_text_input(
array(
'id' => 'custom_price',
'name' => '_custom_price',
'value' => get_post_meta( $thepostid, '_custom_price', true ),
'class' => 'wc_input_price short',
'label' => __( 'Custom Price ($)', 'vg' ),
)
);
}
add_action( 'woocommerce_product_options_pricing', 'show_custom_price_admin' );
/**
* Function to update custom price Admin Product add/edit page
*
* @param int $post_id Product's post id.
*/
function process_product_custom_data( $post_id ) {
$product = wc_get_product( $post_id );
$product->update_meta_data(
'_custom_price',
wc_clean( wp_unslash( filter_input( INPUT_POST, '_custom_price' ) ) )
);
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'process_product_custom_data' );
/**
* Function to show custom price front end page
*/
function show_custom_price_frontend() {
global $post;
$custom_price = get_post_meta( $post->ID, '_custom_price', true );
if ( $custom_price ) {
$custom_price = wc_price( $custom_price );
printf( '<p class="price">%s</p>', $custom_price );
}
}
add_action( 'woocommerce_before_add_to_cart_form', 'show_custom_price_frontend' );