Looking to insert a WooCommerce product price into a layout, well you can add the WooCommerce Product Price as a shortcode using the product ID as an attribute for the correct product.

The code below goes into your functions.php file, the add the shortcode where you need to display the price.

add_shortcode( 'cl_product_price', 'cl_woo_product_price_shortcode' );
/**
 * Shortcode WooCommerce Product Price.
 *
 */
function cl_woo_product_price_shortcode( $atts ) {
	
	$atts = shortcode_atts( array(
		'id' => null
	), $atts, 'cl_product_price' );
 
	if ( empty( $atts[ 'id' ] ) ) {
		return '';
	}
 
	$product = wc_get_product( $atts['id'] );
 
	if ( ! $product ) {
		return '';
	}
 
	return $product->get_price_html();
}

And use the shortcode in the layout/template with the Product ID number as an attribute.

[cl_product_price id="1011"]

9 Comments

  1. Rene on December 13, 2020 at 5:53 pm

    i am asking for too much. can someone pls tell me which functions.php file do i need to update to use the shortcode…there are too many files and i am a not a developer. Thanks in advance!

    • Neil Gowran on December 15, 2020 at 11:12 pm

      It is the functions file that is in the active theme of the site.

  2. Clinton on August 6, 2020 at 4:52 pm

    The Code works Perfect, Thanks
    How to change the font size and color?

  3. Shaun Pan on June 29, 2020 at 1:37 am

    Great contribution work fine using product ID.
    –Dynamic price required instead of ID grab.
    But I am using in blocks lightbox pop up and I have twenty products with different prices, when user click link button the module show the same price for every product, I want see the module display the current product page price on popup blocks.
    Any help will be much appreciated.

  4. Stuart on June 5, 2020 at 10:23 am

    Is there a way to do this dynamically so instead of using an id it grabs it from the product page?

  5. V on May 18, 2020 at 11:00 am

    You already know that woocommerce has 2 prices, normal price and reduced price.
    Could it be that only the discounted price is displayed?

    Thank you!!

  6. V on May 18, 2020 at 10:42 am

    Worked perfectly!!!!

    I´m so happy =) =) =)

    Great thanks!!

  7. Sónia Dias on April 13, 2020 at 11:28 am

    Hi Lewis,

    I’ve tried using elementor but it doen’t work. :(

  8. lewis on March 7, 2020 at 12:31 am

    Worked Perfectly! Thank you for this code.

Leave a Comment