WooCommerce Guide: How to Use It Effectively

Get WooCommerce Product Price: A Beginner’s Guide

Step 1: Accessing Product Data in WooCommerce

Locate the Product ID

To retrieve any product-specific information in WooCommerce, the first crucial step is to identify the unique Product ID. This ID is an integer assigned to each product when it’s created. You can find the Product ID in several ways:

  • From the WordPress Admin Dashboard: Navigate to WooCommerce > Products. Hover over the product you’re interested in, and the Product ID will appear in the URL displayed in your browser’s status bar (e.g., post=123&action=edit, where ‘123’ is the ID). Alternatively, you can click on the product to edit it, and the ID will be visible in the URL of the edit page.
  • Programmatically: If you’re working within a WordPress loop (e.g., on a shop page or category archive), you can often get the current post ID using get_the_ID(). If you have a WC_Product object, you can get its ID using $product->get_id().

Understand Product Data Structures

WooCommerce stores product data in custom post types and custom tables. When you interact with product information programmatically, you’ll primarily be working with the WC_Product object. This object is an instance of a class that holds all the relevant data for a specific product and provides methods to access that data.

There are different types of WC_Product objects depending on the product type:

  • WC_Product_Simple for simple products.
  • WC_Product_Variable for variable products.
  • WC_Product_Grouped for grouped products.
  • WC_Product_External for external/affiliate products.

While you don’t always need to instantiate these specific classes directly (WooCommerce often handles this for you when you retrieve a product by ID), understanding their existence helps in comprehending how different product types behave and store their pricing.

Step 2: Retrieving Simple Product Prices

Using the get_price() Method

For simple products, retrieving the price is straightforward using the WC_Product object’s built-in methods. The primary method you’ll use is get_price().

Here’s a basic example of how to get a simple product’s price:


$product_id = 123; // Replace with your actual product ID
$product = wc_get_product( $product_id );

if ( $product && $product->is_type( 'simple' ) ) {
    $price = $product->get_price(); // Returns the current price, considering sale price if active
    $regular_price = $product->get_regular_price(); // Returns the regular price
    $sale_price = $product->get_sale_price(); // Returns the sale price, or empty string if no sale

    echo "Product Price: " . wc_price( $price ) . "
"; echo "Regular Price: " . wc_price( $regular_price ) . "
"; if ( $sale_price ) { echo "Sale Price: " . wc_price( $sale_price ); } } else { echo "Product not found or not a simple product."; }

Note: wc_get_product( $product_id ) is the recommended function to retrieve a product object by its ID. It handles caching and ensures you get the correct product type object.

Displaying the Price on the Frontend

When displaying prices on the frontend, it’s crucial to use WooCommerce’s formatting functions to ensure currency symbols, decimal places, and thousands separators are correct according to your store’s settings. The most common function for this is wc_price().

Example of displaying prices correctly formatted:


// Assuming $product is a WC_Product object
if ( $product ) {
    // Get the display price (takes sale price into account)
    $display_price = $product->get_price_html(); // Returns formatted price HTML, e.g., "$10.00" or "$15.00 $10.00" for sale

    // Or, if you want just the numerical value and then format it yourself:
    $raw_price = $product->get_price();
    $formatted_raw_price = wc_price( $raw_price );

    echo "Price (HTML formatted): " . $display_price . "
"; echo "Price (Manually formatted): " . $formatted_raw_price; }

get_price_html() is particularly useful as it automatically handles displaying sale prices with the regular price struck out, if applicable, making it ideal for direct output on product pages or listings.

Step 3: Handling Variable Product Prices

Accessing Variation Data

Variable products add complexity because their price can change based on selected attributes (variations). To get the price of a specific variation, you need to access the variation’s data.

Each variation is essentially treated as its own simple product internally, with its own ID, price, SKU, etc. You can retrieve a specific variation by its ID.

Example of accessing a specific variation’s price:


$variable_product_id = 456; // ID of the parent variable product
$variation_id = 457; // ID of a specific variation

$variable_product = wc_get_product( $variable_product_id );
$variation = wc_get_product( $variation_id ); // Get the WC_Product_Variation object

if ( $variable_product && $variable_product->is_type( 'variable' ) && $variation && $variation->is_type( 'variation' ) ) {
    $variation_price = $variation->get_price();
    $variation_regular_price = $variation->get_regular_price();
    $variation_sale_price = $variation->get_sale_price();

    echo "Variation Price: " . wc_price( $variation_price ) . "
"; echo "Variation Regular Price: " . wc_price( $variation_regular_price ) . "
"; if ( $variation_sale_price ) { echo "Variation Sale Price: " . wc_price( $variation_sale_price ); } } else { echo "Product not found, not variable, or variation not found."; }

Determining the Price Range

For variable products, you often need to display a price range (e.g., “$10 – $20”) on the shop page or product archives before a variation is selected. WooCommerce provides methods to get the minimum and maximum prices of all available variations.

Example of getting the price range for a variable product:


$variable_product_id = 456; // ID of the parent variable product
$product = wc_get_product( $variable_product_id );

if ( $product && $product->is_type( 'variable' ) ) {
    $min_price = $product->get_variation_price( 'min', true ); // 'true' for including sale price
    $max_price = $product->get_variation_price( 'max', true );

    $min_regular_price = $product->get_variation_regular_price( 'min', true );
    $max_regular_price = $product->get_variation_regular_price( 'max', true );

    $min_sale_price = $product->get_variation_sale_price( 'min', true );
    $max_sale_price = $product->get_variation_sale_price( 'max', true );

    echo "Price Range: " . wc_price( $min_price ) . " - " . wc_price( $max_price ) . "
"; echo "Regular Price Range: " . wc_price( $min_regular_price ) . " - " . wc_price( $max_regular_price ) . "
"; if ( $min_sale_price && $max_sale_price ) { echo "Sale Price Range: " . wc_price( $min_sale_price ) . " - " . wc_price( $max_sale_price ) . "
"; } // You can also use get_price_html() on the variable product itself to get the formatted range echo "Formatted Price Range: " . $product->get_price_html(); } else { echo "Product not found or not a variable product."; }

get_variation_price() (and its regular/sale counterparts) are powerful for determining the price boundaries. The get_price_html() method on the variable product object itself will automatically generate the correctly formatted price range, including sale price indications.

Step 4: Advanced Price Retrieval Techniques

Applying Filters and Hooks

WooCommerce is highly extensible, and price retrieval is no exception. Filters allow you to modify data before it’s returned, while actions let you execute code at specific points. This is crucial for custom pricing logic, discounts, or integrating with third-party plugins.

Some common filters related to product pricing include:

  • woocommerce_product_get_price: Filters the product’s current price (the one returned by $product->get_price()).
  • woocommerce_product_get_regular_price: Filters the product’s regular price.
  • woocommerce_product_get_sale_price: Filters the product’s sale price.
  • woocommerce_product_variation_get_price: Filters a variation’s price.
  • woocommerce_get_price_html: Filters the HTML string displayed for the product price on the frontend.

Example using woocommerce_product_get_price to add a custom fee:


function custom_product_price_adjustment( $price, $product ) {
    // Apply a $5 surcharge to specific products, or based on some custom logic
    if ( $product->get_id() === 789 ) { // Replace with your product ID
        $price += 5;
    }
    return $price;
}
add_filter( 'woocommerce_product_get_price', 'custom_product_price_adjustment', 10, 2 );

// Example using woocommerce_get_price_html to add custom text
function custom_price_html_info( $price_html, $product ) {
    if ( $product->is_on_sale() ) {
        $price_html .= ' (Limited Time Offer!)';
    } else {
        $price_html .= ' (No hidden fees)';
    }
    return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'custom_price_html_info', 10, 2 );

When using filters, always ensure your function accepts the correct number of arguments (defined by the filter’s documentation) and returns the modified value.

Working with Custom Fields for Pricing

Sometimes, you might store custom pricing information (e.g., wholesale prices, tiered pricing, or a cost price) in custom fields (meta data) associated with your products, often managed by plugins like Advanced Custom Fields (ACF) or directly via WordPress custom fields.

To retrieve prices from custom fields, you’ll use WordPress’s meta data functions, typically get_post_meta(), or the WC_Product object’s get_meta() method.

Example of retrieving a ‘wholesale_price’ custom field:


$product_id = 901; // Your product ID
$product = wc_get_product( $product_id );

if ( $product ) {
    // Using WC_Product object's get_meta()
    $wholesale_price = $product->get_meta( '_wholesale_price_field' ); // Assuming '_wholesale_price_field' is your custom field key

    // Alternatively, using get_post_meta() directly
    $wholesale_price_alt = get_post_meta( $product_id, '_wholesale_price_field', true ); // 'true' returns a single value

    if ( ! empty( $wholesale_price ) ) {
        echo "Wholesale Price (via get_meta()): " . wc_price( $wholesale_price ) . "
"; } if ( ! empty( $wholesale_price_alt ) ) { echo "Wholesale Price (via get_post_meta()): " . wc_price( $wholesale_price_alt ) . "
"; } }

If you need this custom price to replace the default WooCommerce price, you would combine this technique with a filter like woocommerce_product_get_price, as demonstrated in the previous section. This allows you to override or modify the standard pricing logic based on your custom field values.

FAQs

Q1: Why does get_price() return an empty string or 0 for some products?

A: This typically happens for products that are not purchasable or don’t have a price set. Common reasons include:

  • Variable Products: If you call get_price() directly on a parent variable product, it might return an empty string because the price depends on the selected variation. Use get_variation_price() or get_price_html() for ranges.
  • Grouped Products: Grouped products do not have their own price; their price is derived from the sum of their child products.
  • External/Affiliate Products: These products link to an external URL for purchase and often don’t have a price stored in WooCommerce.
  • Draft/Pending Products: Products not yet published might not have their price fully indexed.
  • Missing Price: The regular price field for the product might genuinely be empty in the product data.

Q2: How can I get the price with tax included/excluded?

A: WooCommerce handles tax calculations based on your store’s settings (WooCommerce > Settings > Tax). When retrieving prices programmatically, you can explicitly ask for prices including or excluding tax using the wc_get_price_to_display() function.


$product_id = 123;
$product = wc_get_product( $product_id );
if ( $product ) {
    // Get price including tax
    $price_incl_tax = wc_get_price_to_display( $product, array( 'price' => $product->get_price(), 'qty' => 1, 'tax_display' => 'incl' ) );

    // Get price excluding tax
    $price_excl_tax = wc_get_price_to_display( $product, array( 'price' => $product->get_price(), 'qty' => 1, 'tax_display' => 'excl' ) );

    echo "Price Including Tax: " . wc_price( $price_incl_tax ) . "
"; echo "Price Excluding Tax: " . wc_price( $price_excl_tax ); }

The wc_get_price_to_display() function is robust as it takes into account your tax settings, customer location (if using geolocation), and product tax status.

Q3: What’s the difference between get_price() and get_price_html()?

A:

  • $product->get_price(): Returns the raw, numeric value of the product’s current price (a float or string). This value is what you would use for calculations, database storage, or when you need the pure number. It automatically considers if a sale price is active and returns that, otherwise the regular price.
  • $product->get_price_html(): Returns an HTML string representing the formatted price, ready for display on the frontend. This method handles currency symbols, decimal places, thousands separators, and importantly, it also manages the display of sale prices (e.g., showing the regular price struck out next to the sale price) and price ranges for variable products. It is designed for direct output to the user interface.

In short: use get_price() for logic and calculations, use get_price_html() for display.

Q4: How can I get the price of a product in a specific currency if my store uses a multi-currency plugin?

A: If you are using a multi-currency plugin (like Currency Switcher for WooCommerce, WPML, or others), these plugins typically hook into WooCommerce’s pricing functions using the filters mentioned in “Step 4: Advanced Price Retrieval Techniques”. When you call $product->get_price() or wc_price(), the plugin’s filters will automatically convert the price to the currently selected currency based on its configuration.

To programmatically get a price in a *specific* currency (not necessarily the one currently selected by the user), you would need to consult the documentation of your specific multi-currency plugin. Most plugins provide their own API functions or filters that allow you to pass a target currency code to retrieve the converted price. Directly modifying the price without using the plugin’s API could lead to inconsistencies.

Scroll to Top