Posted on Leave a comment

Get product price by product ID in WooCommerce

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.40486.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;
}
Leave a Reply

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