Product Price Shortcode
Adds a [wpide_product_price id="123"] shortcode that prints the current price of any product, including sale formatting, inside posts and pages.
Opens WPIDE on
Code
add_shortcode( 'wpide_product_price', function ( $atts ) {
$atts = shortcode_atts( array( 'id' => 0 ), $atts, 'wpide_product_price' );
$id = absint( $atts['id'] ) ? absint( $atts['id'] ) : get_the_ID();
$product = $id ? wc_get_product( $id ) : false;
if ( ! $product ) {
return '';
}
return '<span class="wpide-product-price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
} );
Notes
Use the product ID from the Products list. On a product page the id can be omitted.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.