Add “Recommended Retail Price (RRP)” to WooCommerce products with PHP

/**
 * Adds "RRP" admin input field to the "Product data" meta box > "General" tab > Pricing section.
 *
 * @see https://wpcodebook.com/snippets/add-recommended-retail-price-rrp-to-woocommerce-with-php/
 * @see https://github.com/woocommerce/woocommerce/blob/7.9.0/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-general.php#L73
 */
add_action( 'woocommerce_product_options_pricing', function () {
	global $product_object;
	woocommerce_wp_text_input(
		array(
			'id'        => 'rrp',
			'value'     => $product_object->get_meta( 'rrp', true, 'edit' ),
			'label'     => __( 'RRP' ) . ' (' . get_woocommerce_currency_symbol() . ')',
			'data_type' => 'price',
		)
	);
} );

/**
 * Saves "RRP" admin input field in product meta.
 *
 * @see https://wpcodebook.com/snippets/add-recommended-retail-price-rrp-to-woocommerce-with-php/
 * @see https://github.com/woocommerce/woocommerce/blob/7.9.0/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-product-data.php#L422
 */
add_action( 'woocommerce_admin_process_product_object', function ( $product ) {
	if ( isset( $_POST['rrp'] ) ) {
		$product->update_meta_data( 'rrp', wc_clean( wp_unslash( $_POST['rrp'] ) ) );
	}
} );

/**
 * Displays RRP in the product's price.
 *
 * @see https://wpcodebook.com/snippets/add-recommended-retail-price-rrp-to-woocommerce-with-php/
 * @see https://github.com/woocommerce/woocommerce/blob/7.9.0/plugins/woocommerce/includes/abstracts/abstract-wc-product.php#L1884
 */
add_filter( 'woocommerce_get_price_html', function ( $price, $product ) {
	if ( $rrp = $product->get_meta( 'rrp' ) ) {
		$price = sprintf( '%s <span class="rrp">%s: %s</span>', $price, esc_html__( 'RRP' ), wc_price( $rrp ) );
	}
	return $price;
}, 10, 2 );

Leave a Comment