Categories
Woocommerce

How to display product price using shortcode

Here is the code snippet that you can use to create a shortcode and use it in any of your posts or pages to display the price of a specific product. You just have to specify the woocommerce product ID of that product like the given example. [woo_product_price id=20]

function wptips_woo_price_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'woocommerce_price' );

    $html = '';

    if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
         $_product = wc_get_product( $atts['id'] );
         $html = $_product->get_price();
    }
    return $html;
}
add_shortcode( 'woo_product_price', 'wptips_woo_price_shortcode' );

By Murali Kumar

I am a Full Stack Web Developer with over 12 years of experience. I love WordPress & WooCommerce.

If you need the help from a WordPress developer for your website, get in touch now!.

Twitter : @geekz
YouTube : @wptipsofficial

One reply on “How to display product price using shortcode”

I’m not sure if the latest update to WooCommerce broke your code. I get the following fatal error on my front end:

Fatal error: Uncaught Error: Call to a member function get_price() on bool

Leave a Reply

Your email address will not be published. Required fields are marked *