Home » WooCommerce: Add Custom Product Fields (e.g. RRP) Without a Plugin

WooCommerce: Add Custom Product Fields (e.g. RRP) Without a Plugin

by adminkys

The manufacturer’s suggested retail price (MSRP), or the recommended retail price (RRP), is the price at which the manufacturer recommends that the retailer sells the product at. You might have seen this in an ad, on a magazine, on a price tag: “RRP: $50. Our price: $39!”.

WooCommerce entrepreneurs can take advantage of this “marketing trick” too. The only problem is: how do we show this “extra field” on the single product page AND in the product edit page, so that the website owner can add this easily?

WooCommerce: Display RRP/MSRP on the Single Product Page
WooCommerce: Display RRP/MSRP on the Single Product Page
WooCommerce: add RRP/MSRP field input @ product edit page
WooCommerce: add RRP/MSRP field input @ product edit page

PHP Snippet: Add RRP/MSRP @ WooCommerce Single Product Page

/**
 * @snippet       Display RRP/MSRP @ WooCommerce Single Product Page
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://www.tutoraspire.com
 */
 
// -----------------------------------------
// 1. Add RRP field input @ product edit page
 
add_action( 'woocommerce_product_options_pricing', 'tutoraspire_add_RRP_to_products' );      
 
function tutoraspire_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', 
    ));      
}
 
// -----------------------------------------
// 2. Save RRP field via custom field
 
add_action( 'save_post_product', 'tutoraspire_save_RRP' );
 
function tutoraspire_save_RRP( $product_id ) {
    global $typenow;
    if ( 'product' === $typenow ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if ( isset( $_POST['rrp'] ) ) {
            update_post_meta( $product_id, 'rrp', $_POST['rrp'] );
        }
    }
}
 
// -----------------------------------------
// 3. Display RRP field @ single product page
 
add_action( 'woocommerce_single_product_summary', 'tutoraspire_display_RRP', 9 );
 
function tutoraspire_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 '
'; } }

You may also like