You can convert the function that actually does the output into a shortcode like this:
add_shortcode( 'single_product_summary', function () {
global $product;
$output = '';
if ( $product && $product->get_type() <> 'variable' && $rrp = get_post_meta( $product->get_id(), 'rrp', true ) ) {
$output .= '<div class="woocommerce_rrp">';
$output .= __( 'RRP: ', 'woocommerce' );
$output .= '<span>' . wc_price( $rrp ) . '</span>';
$output .= '</div>';
}
return $output;
} );
I’d imagine that the functions that handle displaying the product options field and saving its data can remain the same.
Thread Starter
lbwdev
(@lbwdev)
Thank you. When i want to then use the short code what do i put as the shortcode on the page? eg [shortcode] i’m not sure what text goes there
You would use the [single_product_summary] shortcode in an elementor widget to have it show up.
Thread Starter
lbwdev
(@lbwdev)
I can’t seem to get it to work – can you confirm the code is correct is this line supposed to be blank in the commas? $output = ”;
When i add this to the snippet nothing shows up when i put the shortcode [single_product_summary]
I replaced the output section on the original snippet with the code supplied above. can you let me know if that was the correct thing to do?
I replaced this section:
// 3. Display RRP field @ single product page
add_action( ‘woocommerce_single_product_summary’, ‘bbloomer_display_RRP’, 9 );
function bbloomer_display_RRP() {
global $product;
if ( $product->get_type() <> ‘variable’ && $rrp = get_post_meta( $product->get_id(), ‘rrp’, true ) ) {
echo ‘<div class=”woocommerce_rrp”>’;
_e( ‘RRP: ‘, ‘woocommerce’ );
echo ‘<span>’ . wc_price( $rrp ) . ‘</span>’;
echo ‘</div>’;
}
}`
With this code:
add_shortcode( ‘single_product_summary’, function () {
global $product;
$output = ”;
if ( $product && $product->get_type() <> ‘variable’ && $rrp = get_post_meta( $product->get_id(), ‘rrp’, true ) ) {
$output .= ‘<div class=”woocommerce_rrp”>’;
$output .= __( ‘RRP: ‘, ‘woocommerce’ );
$output .= ‘<span>’ . wc_price( $rrp ) . ‘</span>’;
$output .= ‘</div>’;
}
return $output;
} );`