I've managed to get this together for simple products but I need to work with variable products as well. This creates a custom product field on the product page (WC backend) and the text is saved as "RRP / MSRP" which stands for Recommended Retail Price / Manufacturer Suggested Retail Price.
The price will be displayed above the product price on the product page. My problem, as already explained, is that this only works for simple products. I need help making it work for all product types, but most importantly - variable products.
The code:
add_action( 'woocommerce_product_options_pricing', 'add_msrp' );
function add_msrp() {
woocommerce_wp_text_input( array(
'id' => 'msrp',
'class' => 'short wc_input_price',
'label' => __( 'RRP / MSRP', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
'data_type' => 'price',
) );
}
add_action( 'save_post_product', 'save_msrp' );
function save_msrp( $product_id ) {
global $pagenow, $typenow;
if ( 'post.php' !== $pagenow || 'product' !== $typenow )
return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['msrp'] ) ) {
if ( $_POST['msrp'] )
update_post_meta( $product_id, 'msrp', $_POST['msrp'] );
} else
delete_post_meta( $product_id, 'msrp' );
}
add_action( 'woocommerce_single_product_summary', 'display_msrp', 9 );
function display_msrp() {
global $product;
if ( $product->get_type() <> 'variable' && $msrp = get_post_meta( $product->get_id(), 'msrp', true ) ) {
echo '<div class="woocommerce_msrp">';
_e( 'RRP / MSRP: ', 'woocommerce' );
echo '<span class="msrp-price">' . wc_price( $msrp ) . '</span>';
echo '</div>';
}
}
Can someone please help me with this?
Your code is a bit outdated since WooCommerce 3+ and some other things like the meta_key slugs to be used for custom fields (I have revisited it at the end)...
To handle product variations from variable product, use the following:
// Backend Variation - Add / Display MRSP Field
add_action( 'woocommerce_variation_options_pricing', 'add_variation_options_pricing_msrp', 10, 3 );
function add_variation_options_pricing_msrp( $loop, $variation_data, $variation ){
woocommerce_wp_text_input( array(
'id' => '_msrp_'.$loop,
'wrapper_class' => 'form-row form-row-first',
'class' => 'short wc_input_price',
'label' => __( 'RRP / MSRP', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
'value' => wc_format_localized_price( get_post_meta( $variation->ID, '_msrp', true ) ),
'data_type' => 'price',
) );
}
// Backend Variation - Save MRSP Field value
add_action( 'woocommerce_save_product_variation','save_variation_options_pricing_msrp',10 ,2 );
function save_variation_options_pricing_msrp( $variation_id, $loop ){
if( isset($_POST['_msrp_'.$loop]) )
update_post_meta( $variation_id, '_msrp', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_msrp_'.$loop] ) ) ) );
}
// Frontend Variation - MRSP display
add_filter( 'woocommerce_available_variation', 'display_variation_msrp', 10, 3 );
function display_variation_msrp( $data, $product, $variation ) {
if( $msrp = $variation->get_meta('_msrp') ) {
$data['price_html'] = '<div class="woocommerce_msrp">' . __( 'RRP / MSRP: ', 'woocommerce' ) .
'<span class="msrp-price">' . wc_price( $msrp ) . '</span></div>' . $data['price_html'];
}
return $data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
For all other products.
Here below your revisited code (product metakeys should always start with an underscore):
// Backend Product - Add / Display MRSP Field
add_action( 'woocommerce_product_options_pricing', 'add_product_options_pricing_msrp' );
function add_product_options_pricing_msrp() {
global $product_object;
woocommerce_wp_text_input( array(
'id' => '_msrp',
'class' => 'short wc_input_price',
'label' => __( 'RRP / MSRP', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
'data_type' => 'price',
'value' => str_replace('.', ',', $product_object->get_meta('_msrp') ),
) );
}
// Backend Product - Save MRSP Field value (since Woocommerce 3)
add_action( 'woocommerce_admin_process_product_object', 'save_product_options_pricing_msrp', 10, 1 );
function save_product_options_pricing_msrp( $product ) {
if ( isset( $_POST['_msrp'] ) )
$product->update_meta_data( '_msrp', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_msrp'] ) ) ) );
}
// Frontend Product - MRSP display
add_action( 'woocommerce_single_product_summary', 'display_product_msrp', 9 );
function display_product_msrp() {
global $product;
if ( $msrp = $product->get_meta('_msrp') ) {
echo '<div class="woocommerce_msrp">' . __( 'RRP / MSRP: ', 'woocommerce' );
echo '<span class="msrp-price">' . wc_price( $msrp ) . '</span>
</div>';
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.