Site icon Experience Wordpress Developer Online – Design Develop and Maintenance Support

Add Custom Field to Single Product Page WooCommerce

In WooCommerce, If you want to add custom field in single product page than here is solutions. In this example i was add recommended retail price(RRP). It’s price which manufacturer recommends retailer to sell the product. It will display in any Ads or magazine. Like “RRP : ₹500 ” and main price : ₹ 200

So here is code. Add below line of code in your theme’s functions.php file of your active child theme (or theme) and save the file.


// Add RRP field input at product edit page 

add_action( 'woocommerce_product_options_pricing', 'add_RRP_to_products' );        
 
function add_RRP_to_products() {           
woocommerce_wp_text_input( array( 
'id' => 'rrp', 
'class' => 'short wc_input_price', 
'label' => __( 'RRP', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
'data_type' => 'price', 
) 
);      
}
 
// Save RRP field via custom field
 
add_action( 'save_post', 'save_RRP' );
 
function save_RRP( $product_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['rrp'] ) ) {
        if ( is_numeric( $_POST['rrp'] ) )
            update_post_meta( $product_id, 'rrp', $_POST['rrp'] );
    } else delete_post_meta( $product_id, 'rrp' );
}

// Display RRP field at single product page
 
add_action( 'woocommerce_single_product_summary', 'display_RRP', 9 );
 
function display_RRP() {
    global $product;
     
    if ( $product->get_type()  'variable' && $rrp = get_post_meta( $product->get_id(), 'rrp', true ) ) {
        echo '
'; _e( 'RRP: ', 'woocommerce' ); echo '' . wc_price( $rrp ) . ''; echo '
'; } }
Exit mobile version