A Sloth coding for WooCommerce on his laptop
No Comments

Displaying all possible prices for a WooCommerce product

Camilla Monk designs sleek websites for book industry professionals and writes high-octane nonsense. She lives in Montréal, where she feeds the squirrels and tries to raise a toddler.

A Sloth coding for WooCommerce on his laptop

If, like me, you need to code a custom display of WooCommerce product prices, but you’re barely competent at PHP, here’s a full recap to help you:

  1. Display your WooCommerce store’s currency (assuming you only have one. Let’s save the multi-currency nightmare for another day)
  2. Display a WooCommerce simple product’s regular price without currency (i.e.: 4.99)
  3. Display a WooCommerce simple product’s sale price without currency (i.e.: 2.99)
  4. Display a WooCommerce product’s default price (the full purchase price, i.e.: $4.99)
  5. Quickly display a WooCommerce product’s regular and sale price with currency (i.e.: $4.99 $2.99)
  6. Display a WooCommerce product variation’s regular price without currency (i.e.: 4.99)
  7. Display a WooCommerce product variation’s sale price without currency (i.e.: 2.99)
  8. Quickly display a WooCommerce product variation’s regular and sale price with currency (i.e.: $4.99 $2.99)
  9. Advanced: Display a WooCommerce product variation’s lowest and highest price (i.e.: $4.99 – $7.99)
  10. Ain’t nobody got time for that: jump to a full piece of code that works

Without further ado…

Display a WooCommerce simple product variation’s prices

Before we begin…

Let’s start with calling a given product:
I’m going to assign an arbitrary ID to a variable, and use the wc_get_product() method to return the product as an object.

$product_id = [any integer. Ex: 33125];
$my_product = wc_get_product( $product_id ); 

1) Display your WooCommerce store’s currency

Now, before retrieving product prices, we need the currency. WooCommerce returns prices as simple strings without a currency symbol (Ex: 7.99), unless you use the price_html attribute (more on that later.) So we need to store the currency in a variable, and for that, we can use either:

get_option('woocommerce_currency')

This returns the three-letter currency code that is set in the WooCommerce settings. (e.g., USD for U.S. Dollar, EUR for Euro).

Or we can use:

get_woocommerce_currency_symbol()

This returns the currency symbol associated with the currency code set in the WooCommerce settings. I’ll go with that second one to display my prices:

$currency = get_woocommerce_currency_symbol();

Now that this is set, if we’re dealing with a simple product, we can use three methods to:

2) Display a WooCommerce simple product’s regular price without currency (i.e.: 4.99)

In short, use: get_regular_price(), which will return the regular price, without currency (Example: 7.99.) To display the regular price along with the currency, you’ll do:

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

$my_regular_price = $my_product->get_regular_price();

//And if we prepend the currency:
$my_regular_price = $currency.$my_product->get_regular_price();

get_regular_price() will return an empty string if the product is variable, because the parent product has no price of its own.

3) Display a WooCommerce simple product’s sale price without currency (i.e.: 2.99)

For the sale price, use: get_sale_price(), which will return the sale price, without currency (Example: 4.99.) To display the sale price along with the currency, you’ll do:

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

$my_sale_price = $my_product->get_sale_price();

//And if we prepend the currency:
$my_sale_price = $currency.$my_product->get_sale_price();

get_sale_price() will return an empty string if the product is variable, because the parent product has no price of its own.

All of this is very useful if you want to return something like: $7.99 $4.99 to signal a promotion, but if you want a shortcut to display the full purchase price, whether it’s regular or on sale, you can do this:

4) Display a WooCommerce product’s default price (the full purchase price, i.e.: $4.99)

For the default price, use: get_price(), which will return the final purchase price, without currency (either the regular price, or, if applicable, the sale price.)

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

$my_default_price = $my_product->get_price();

//And if we prepend the currency:
$my_default_price = $currency.$my_product->get_price();

If the product is variable, get_price() will return the default variation price.

5) Quickly display of a WooCommerce product’s regular and sale price with currency (i.e.: $4.99 $2.99)

Lastly, here’s a shortcut method to display both the regular and sale price, along with the currency, all in one go: get_price_html()

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

$my_default_price = $my_product->get_price_html();

//returns: $4.99 $2.99

And the HTML code returned will be:

<span class="price">
	<del aria-hidden="true">
		<span class="woocommerce-Price-amount amount">
			<bdi>
				<span>$</span>19.99
			</bdi>
		</span>
	</del> <ins><span class="woocommerce-Price-amount amount">
		<bdi>
			<span class="woocommerce-Price-currencySymbol">$</span>14.99
		</bdi>
	</span></ins>
</span>

Remarks:

  • Mind the space between </del> <ins>, which could pose styling issues.
  • <del> is styled by WooCommerce with the following CSS property: text-decoration: line-through;

To sum it up, on a variable product, get_regular_price() and get_sale_price() will return an empty string, whereas get_price() will display something as long as at least one variation exists and has a price, and get_price_html() will return the price and currency, all fully formatted and including the sale price, if any.

Now let’s move on and…

Display a WooCommerce product variation’s prices

Retrieve WooCommerce product variations, by ID

To retrieve all variation prices, we’ll first need to retrieve those variations. First, we’ll check if the product is variable, using the is_type() method. You can pass the arguments ‘simple’ or ‘variable’ to achieve that.

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

if ( $my_product->is_type( 'variable' ) ) {
	//Code here...
}

Next, we’ll use the get_available_variations() method to return an array of all variations.

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

if ( $my_product->is_type( 'variable' ) ) {
	$product_variations = $my_product->get_available_variations();
}

We will then loop through the product variations array.

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

if ( $my_product->is_type( 'variable' ) ) {
	$product_variations = $my_product->get_available_variations();
	foreach ($product_variations as $variation) {
		//Code here
	}
}

We’re now inside a variation’s list of attributes, and we can:

6) Display a WooCommerce product variation’s regular price without currency (i.e.: 4.99)

The attribute for that is display_regular_price. Note that the methods we used for simple products will not work to retrieve array values. Here’s how we retrieve our price:

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

if ( $my_product->is_type( 'variable' ) ) {
	$product_variations = $my_product->get_available_variations();
	foreach ($product_variations as $variation) {		//Echo the variations's regular price
		echo $variation['display_regular_price'];
	}
}

7) Display a WooCommerce product variation’s sale price without currency (i.e.: 4.99)

This is a little counter-intuitive, but to get the variation’s sale price, you need to use the attribute display_price. This means that we’re showing the final purchase price, which is the sale price if one has been set. Here’s the full code:

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

if ( $my_product->is_type( 'variable' ) ) {
	$product_variations = $my_product->get_available_variations();
	foreach ($product_variations as $variation) {		//Echo the variations's sale price
		echo $variation['display_price'];
	}
}

Now, if you don’t need any fancy display, here’s a shortcut that will return both the price and currency:

8) Quickly display a WooCommerce product variation’s regular and sale price with currency (i.e.: $4.99 $2.99)

Finally, just like for a simple product, you can use price_html, this time as an attribute in the array returned by get_available_variations().

See #5 above for what this returns.

$my_product = wc_get_product( $product_id );
$currency = get_woocommerce_currency_symbol();

if ( $my_product->is_type( 'variable' ) ) {
	$product_variations = $my_product->get_available_variations();
	foreach ($product_variations as $variation) {		//Echo the variations's sale price
		echo $variation['display_price_html'];
	}
}
Previous Post
8 Tips for Authors to Boost Their Homepage

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

0
    0
    Your Cart
    Your cart is empty