Read the code with Rodolfo Melogli! This week in WooCommerce functions we’re looking at get_variation_prices.
If you’re a WooCommerce developer, variable products are always difficult to deal with. Manipulating them with code snippets or custom plugins is even tougher.
Today’s core function (get_variation_prices
) will definitely help you save time while working with prices, custom notices, conditional logic and such.
As usual, we’ll study the function code, take a look at its usage, and finally analyze a case study that will come in useful sooner or later. Enjoy!
Function Code
You can find get_variation_prices under \woocommerce\includes\class-wc-product-variable.php
:
/**
* Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale.
*
* @param bool $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes).
* @return array Array of RAW prices, regular prices, and sale prices with keys set to variation ID.
*/
public function get_variation_prices( $for_display = false ) {
$prices = $this->data_store->read_price_data( $this, $for_display );
foreach ( $prices as $price_key => $variation_prices ) {
$prices[ $price_key ] = $this->sort_variation_prices( $variation_prices );
}
return $prices;
}
By reading the comments, we can see this function returns an array of prices for each variation for a product. This is handy — you don’t need to loop over variations yourself!
It also mentions a parameter, $for_display
, which if set to ‘true’ can return prices according to the store’s tax settings — including or excluding tax, based on whatever option you picked.
In the function code, we see two statements:
read_price_data
clearly gets prices [regular, sale and display] from all variation IDs.sort_variation_prices
is not so clear — but it sorts the returned array from lowest to greatest price. This is helpful when you need to get the min or the max value, for example, as you can just read the first or the last array values!
Function Usage
As long as you have access to the $product global
(in a product loop, on the single product page, in the admin, etc.), you can call get_variation_prices
.
Of course, the product must be a variable one, otherwise the function will return an empty array. (Or maybe an error, who knows? Post a comment if you want to send me some feedback about this.)
To be sure we’re on the single product page and I’m looking at a variable product, we can use the woocommerce_before_variations_form
hook — evidently this triggers only there and then:
add_action( 'woocommerce_before_variations_form', 'bbloomer_get_variation_prices' );
function bbloomer_get_variation_prices() {
global $product;
echo '<pre>';
print_r( $product->get_variation_prices() );
echo '</pre>';
}
Result:
In the grey box (preformatted text thanks to the <pre>
HTML tag), we find the get_variation_prices
output: a multidimensional array with 3 keys (price, regular price, sale price) and array values (variation ID => price amount) sorted by price amount.
Super handy!
Lazy tip alert! You could even use get_variation_prices
to simply get the variation IDs:
$output = $product->get_variation_prices();
$variation_ids = array_keys( $output['price'] );
Printed output for $variation_ids:
Case Study
Let’s wrap up with a practical example.
Before customers choose the variation from the attribute dropdown/s, it would be awesome to show a notice to display the most convenient offer.
We could look, for example, at the variation that has the biggest discount, and entice users to add it to cart today.
We have access to regular prices and sale prices. Each of them are related to a variation ID. Here’s the suggested implementation:
/**
* @snippet Most convenient variation | WooCommerce Single Product
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 6
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( 'woocommerce_before_variations_form', 'bbloomer_most_convenient_variation' );
function bbloomer_most_convenient_variation() {
global $product;
$discount = array();
$output = $product->get_variation_prices();
foreach ( $output['sale_price'] as $variation_id => $sale_price ) {
$regular_price = $output['regular_price'][$variation_id];
$discount[$variation_id] = ( $regular_price - $sale_price ) / $regular_price;
}
$most_conv_variation = array_search( max( $discount), $discount );
$variation = wc_get_product( $most_conv_variation );
echo '<div class="woocommerce-message">Save an amazing ' . max( $discount) . ' when you purchase ' . $variation->get_name() . '!</div>';
}
And here you go with the screenshot.
Of course, at least one variation must be on sale for the snippet to work. You could check if $product->is_on_sale()
to make sure.
Let me know what you learn working with get_variation_prices
in your own project!