Created
May 23, 2019 16:44
-
-
Save mommaroodles/e5a3abc8a1b9472dacf84c40aa6747fb to your computer and use it in GitHub Desktop.
adds MSRP price to woocommerce admin and display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_variation_options_pricing', 'bdev_add_variation_msrp', 11, 3 ); | |
function bdev_add_variation_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' => str_replace('.', ',', get_post_meta($variation->ID, '_msrp', true)), | |
'data_type' => 'price', | |
) ); | |
} | |
add_action( 'woocommerce_save_product_variation','bdev_add_variation_msrp_save',11 ,2 ); | |
function bdev_add_variation_msrp_save( $variation_id, $loop ){ | |
if( isset($_POST['_msrp_'.$loop]) ) | |
update_post_meta( $variation_id, '_msrp', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_msrp_'.$loop] ) ) ) ); | |
} | |
add_filter( 'woocommerce_available_variation', 'bdev_add_variation_msrp_display', 11, 3 ); | |
function bdev_add_variation_msrp_display( $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; | |
} | |
add_action( 'woocommerce_product_options_pricing', 'bdev_add_msrp_to_product' ); | |
function bdev_add_msrp_to_product() { | |
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') ), | |
) ); | |
} | |
add_action( 'woocommerce_admin_process_product_object', 'bdev_add_msrp_to_product_save', 10, 1 ); | |
function bdev_add_msrp_to_product_save( $product ) { | |
if ( isset( $_POST['_msrp'] ) ) | |
$product->update_meta_data( '_msrp', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_msrp'] ) ) ) ); | |
} | |
add_action( 'woocommerce_single_product_summary', 'bdev_add_msrp_to_product_display', 9 ); | |
function bdev_add_msrp_to_product_display() { | |
global $product; | |
if ( $msrp = $product->get_meta('_msrp') ) { | |
echo '<span class="woocommerce_msrp">' . __( 'RRP / MSRP: ', 'woocommerce' ); | |
echo '' . wc_price( $msrp ) . ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment