Coding Snippets June 6, 2018 2 min read

Get WooCommerce Product Price by ID (PHP): get_price(), get_regular_price() & get_sale_price()

Get WooCommerce Product Price by ID (PHP): get_price(), get_regular_price() & get_sale_price()

Quick answer: Load the product with wc_get_product( $product_id ), then call get_price() for the current price, get_regular_price() or get_sale_price() for the individual prices, or get_price_html() for the formatted price. The snippets below wrap each call in a reusable function, including versions with and without tax.

Final Product Price

Use this snippet, if you need to retrieve WooCommerce product’s final price by product’s (i.e. post’s) ID. For variable products, the snippet will return the lowest variation price. The returned price will be “raw”, i.e. not formatted, without currency symbol, etc. You can use the wc_price() function to format the final price.

/**
 * Get product price by product ID.
 */
function wc_get_product_price( $product_id ) {
    return ( $product = wc_get_product( $product_id ) ) ? $product->get_price() : false;
}

Regular & Sale Product Prices

Please note that this will return an empty result for variable products:

/**
 * Get product *regular* price by product ID.
 */
function wc_get_product_regular_price( $product_id ) {
    return ( $product = wc_get_product( $product_id ) ) ? $product->get_regular_price() : false;
}
/**
 * Get product *sale* price by product ID.
 */
function wc_get_product_sale_price( $product_id ) {
    return ( $product = wc_get_product( $product_id ) ) ? $product->get_sale_price() : false;
}

Product Prices Including & Exluding Taxes

As with the final price snippet, this will return the lowest variation price for variable products:

/**
 * Get product price *including tax* by product ID.
 */
function wc_get_product_price_incl_tax( $product_id ) {
    return ( $product = wc_get_product( $product_id ) ) ? wc_get_price_including_tax( $product ) : false;
}
/**
 * Get product price *excluding tax* by product ID.
 */
function wc_get_product_price_excl_tax( $product_id ) {
    return ( $product = wc_get_product( $product_id ) ) ? wc_get_price_excluding_tax( $product ) : false;
}

Product Price HTML

For simple products, it will return price with currency symbol and stricken out sale price if the product is on sale, e.g. €242.00 €121.00. For variable products, it will return price range if variation prices are different, e.g. €244.40 – €486.40:

/**
 * Get product price *HTML* by product ID.
 */
function wc_get_product_price_html( $product_id ) {
    return ( $product = wc_get_product( $product_id ) ) ? $product->get_price_html() : false;
}
all access · lifetime

The plugins behind most of what we write about.

A lot of the posts here reference WPFactory plugins because that's what we build with. All Access gets you every plugin we make, on as many sites as you run. Pay once. Own it.

lifetime, once
$ 499 once
vs. $59.99/yr per plugin, break-even after 9 plugins
  • Every WPFactory plugin, current and future
  • Unlimited sites, no per-site caps
  • Lifetime updates, no renewals
  • 30-day refund, no questions asked