Add Product Price in WooCommerce as a Shortcode

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"]

15 Comments

  1. MR Tran on December 14, 2021 at 1:32 pm

    I inserted the functions.php file but got the whole site error. You can specify the location to insert into the functions file

  2. Rafael on October 23, 2021 at 8:26 am

    this doesnt work for me. instead i use this code:

    function custom_price_shortcode_callback( $atts ) {

    $atts = shortcode_atts( array(
    ‘id’ => null,
    ), $atts, ‘product_price’ );

    $html = ”;

    if( intval( $atts[‘id’] ) > 0 && function_exists( ‘wc_get_product’ ) ){
    // Get an instance of the WC_Product object
    $product = wc_get_product( intval( $atts[‘id’] ) );

    // Get the product prices
    $price = wc_get_price_to_display( $product, array( ‘price’ => $product->get_price() ) ); // Get the active price
    $regular_price = wc_get_price_to_display( $product, array( ‘price’ => $product->get_regular_price() ) ); // Get the regular price
    $sale_price = wc_get_price_to_display( $product, array( ‘price’ => $product->get_sale_price() ) ); // Get the sale price

    // Your price CSS styles
    $style1 = ‘style=”font-size:40px;color:#e79a99;font-weight:bold;”‘;
    $style2 = ‘style=”font-size:25px;color:#e79a99″‘;

    // Formatting price settings (for the wc_price() function)
    $args = array(
    ‘ex_tax_label’ => false,
    ‘currency’ => ‘EUR’,
    ‘decimal_separator’ => ‘.’,
    ‘thousand_separator’ => ‘ ‘,
    ‘decimals’ => 2,
    ‘price_format’ => ‘%2$s %1$s’,
    );

    // Formatting html output
    if( ! empty( $sale_price ) && $sale_price != 0 && $sale_price < $regular_price )
    $html = "” . wc_price( $regular_price, $args ) . “ ” . wc_price( $sale_price, $args ) . “”; // Sale price is set
    else
    $html = “” . wc_price( $price, $args ) . “”; // No sale price set
    }
    return $html;
    }
    add_shortcode( ‘product_price’, ‘custom_price_shortcode_callback’ );

  3. Fabio on August 15, 2021 at 2:22 pm

    Can I apply a CSS style to this code?
    I tried:
    .woocommerce-Price-amount amount{
    color: #FF0000 !important;
    background: #FFFFFF !important;
    }
    but it didn’t work. Your help would be much appreciated.

    Cheers,
    Fabio

    • Neil Gowran on August 18, 2021 at 11:24 pm

      You missing a class prefix ‘.’ for ‘amount’ should be .amount

  4. hamedshh on August 3, 2021 at 5:27 pm

    is there any shotcode that without needing to add id being the price amount
    and is the a shortcode that bring the price amount and currency symbol separately
    I want to use two title boxes, one for price amount and one for price currency symbol

  5. Waleed on June 16, 2021 at 12:29 pm

    What about sale/original price? can I have those separate?

  6. 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.

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

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

  8. 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.

  9. 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?

  10. 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!!

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

    Worked perfectly!!!!

    I´m so happy =) =) =)

    Great thanks!!

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

    Hi Lewis,

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

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

    Worked Perfectly! Thank you for this code.

Leave all Comment