Variation Prices developer reference

Filters for choosing which products Variation Prices affects and changing its price format and markup.

Beka Rice Avatar

Written by

Last updated


Variation Prices’ filters let developers choose which products it affects, pick a different format per product, and change the price markup. Add the snippets below in a code snippets plugin or your child theme’s functions.php. For everyday setup, see Get started with Variation Prices.

When you’d use this

  • Use a different format on some products or pages, such as "Up to" in shop lists and a full list on product pages.
  • Turn the plugin off for certain products or categories.
  • Add your own classes or wrapper markup around the price.
  • Apply your custom format to simple products as well.

Filters

wc_variation_prices_is_product_affected

Whether Variation Prices formats a product’s price. Arguments: $is_product_affected (bool) and $product (WC_Product). Return false to leave a product’s price alone:

add_filter( 'wc_variation_prices_is_product_affected', function( $is_product_affected, $product ) {
	if ( has_term( 'clearance', 'product_cat', $product->get_id() ) ) {
		return false;
	}

	return $is_product_affected;
}, 10, 2 );

To use the format only on the shop page, and there only for the products Variation Prices already affects:

add_filter( 'wc_variation_prices_is_product_affected', function( $affected ) { return $affected && is_shop(); } );

wc_variation_prices_display_format

The format for a product, overriding Price display format. Arguments: $format (string) and $product (WC_Product or null). Return default (price range), from, plus, upto, list or custom.

add_filter( 'wc_variation_prices_display_format', function( $format, $product ) {
	// "Up to" in product lists.
	if ( did_action( 'woocommerce_before_shop_loop_item' ) > did_action( 'woocommerce_after_shop_loop_item' ) ) {
		$format = 'upto';
	}

	// No long lists for products with more than 10 variations.
	if ( $product && 'list' === $format && count( $product->get_children() ) > 10 ) {
		$format = 'default';
	}

	return $format;
}, 10, 2 );

The script that updates the main price on the product page uses the format from the settings, so keep the settings’ format on single product pages.

wc_variation_prices_price_range_format

The template for the price, after the format is chosen. %1$s is replaced with the lowest price and %2$s with the highest. Arguments: $price_range_format (string) and $product.

add_filter( 'wc_variation_prices_price_range_format', function( $price_range_format, $product ) {
	return 'Between %1$s and %2$s';
}, 10, 2 );

wc_variation_prices_price_range

The lowest and highest price used for a product, as [ 'min' => …, 'max' => … ]. Arguments: $range and $product.

wc_variation_prices_min_max_price

A formatted lowest or highest price. Arguments: $price (HTML), $product, $min_or_max (min or max) and $format.

wc_variation_prices_price_range_html

The whole price HTML for a product, replacing Variation Prices’ output. Arguments: $html and $product.

add_filter( 'wc_variation_prices_price_range_html', function( $html, $product ) {
	return $html . '<small class="price-note">Prices vary by size</small>';
}, 10, 2 );

wc_variation_prices_price_range_classes and wc_variation_prices_price_range_attributes

Classes (array of strings) and HTML attributes (array of name => value) for the element around the price. Arguments: the array and $product.

add_filter( 'wc_variation_prices_price_range_classes', function( $classes, $product ) {
	$classes[] = 'my-price';

	return $classes;
}, 10, 2 );

wc_variation_prices_list_tagname

The tag around the List all variation prices list. Default span; div and ul also work.

add_filter( 'wc_variation_prices_list_tagname', function( $tag_name ) {
	return 'div';
} );

wc_variation_prices_list_item_template

The HTML template for one row of the List all variation prices list. It must contain %1$s (the variation ID), %2$s (the variation’s options) and %3$s (its price). The filter passes only the template:

add_filter( 'wc_variation_prices_list_item_template', function( $html ) {
	return '<span class="wvp-list-item" data-product_id="%1$s"><strong>%2$s</strong>: <span class="price">%3$s</span></span>';
} );

wc_variation_prices_list_html

The finished list HTML. Arguments: $html and $product.

wc_variation_prices_price_list

The prices listed, before the list is built. Arguments: $prices (price and regular_price arrays keyed by variation or product ID) and $product.

wc_variation_prices_subproduct_price

One row’s price HTML in the list. Arguments: $price and $child (the variation or grouped product).

wc_variation_prices_show_selected

Whether the main price updates as shoppers pick options, overriding Show selected variation price. Argument: $show_selected (bool).

wc_variation_prices_apply_single_price_format

Variation Prices only formats variable, grouped and variable subscription products. With the Custom format, return true to also show other products, such as simple products, with Custom format when all prices are the same. Arguments: $apply (bool) and $product.

add_filter( 'wc_variation_prices_apply_single_price_format', '__return_true' );

wc_variation_price_single_format and wc_variation_prices_single_price_html

wc_variation_price_single_format filters the template used for those single-price products (%1$s is the price). wc_variation_prices_single_price_html filters their finished price HTML, with $product as the second argument.

woocommerce_get_settings_variation-prices

Filters the fields on WooCommerce » Settings » Products » Variation prices.

Removed in version 2.1.0

The wvp() function and the legacy Util class are gone. Code that called them needs the filters above instead.

Keep learning