[Dev] Variation Prices for WooCommerce

Variation Prices for WooCommerce contains a number of hooks that developers can use to customize the plugin’s behavior.

Filters

wc_variation_prices_is_product_affected

Filter that allows you to override whether the plugin has any effect on a given product or under specific conditions. Returns a boolean.

add_filter( 'wc_variation_prices_is_product_affected', function( $is_product_affected, $product ) {
    // do something to alter $is_product_affected;
    return $is_product_affected;
}, 10, 2 );

The following code snippet would make the plugin effective only on the Shop page.

add_filter( 'wc_variation_prices_is_product_affected', 'is_shop' );

Please consider that it is safe to use the WooCommerce conditional function is_shop as a callback function name in this case because, by the time the filter is invoked, Variation Prices has already verified whether WooCommerce is active or not. Thus, there is no need to check the existence of that function.

wc_variation_prices_price_range_html

Filter that allows you to override the HTML output of the plugin. This filter completely overrides the output of WooCommerce Variations Prices. Returns a string.

add_filter( 'wc_variation_prices_price_range_html', function( $html, $product ) {
    // do something to alter $html;
    return $html;
}, 10, 2 );

wc_variation_prices_price_range_classes

Filter that allows you to add your custom classes to the element enclosing the price range. Returns an array of strings.

add_filter( 'wc_variation_prices_price_range_classes', function( $classes, $product ) {
    $classes[] = 'your-custom-class';
    return $classes;
}, 10, 2 );

wc_variation_prices_list_tagname

Filter that allows you to override the tagName used to enclose the list of variations when the option “List of all variations” is selected. The default tagName is span. Returns a string.

add_filter( 'wc_variation_prices_list_tagname', function( $tagname ) {
    // do something to alter $tagname;
    return $tagname;
} );

wc_variation_prices_list_item_template

Filter that allows you to override the HTML template of the single list item, when “List of all variations” is the selected display format.  The item template must contain three placeholders that will be replaced with:

  • %1$s, the variation id
  • %2$s, the variation name
  • %3$s, the variation price.

The filter returns a string.

add_filter( 'wc_variation_prices_list_item_template', function( $html, $product ) {
    // create your HTML template and assign it to $html
    return $html;
}, 10, 2 );

wc_variation_prices_list_html

Filter that allows you to override the HTML markup of the list of variations. Returns a string.

add_filter( 'wc_variation_prices_list_html', function( $html, $product ) {
    // do something to alter $html;
    return $html;
}, 10, 2 );

wc_variation_prices_display_format

Filter that allows you to override the display format selected in the settings. Returns a string, that should be one of the following values: defaultfromplusuptolist or custom.

add_filter( 'wc_variation_prices_display_format', function( $format, $product ) { 
    // do something to alter $format
    return $format;
}, 10, 2 );

This filter can be used to select a format, different from the global one selected in the settings, depending on the product or other criteria.

add_filter( 'wc_variation_prices_display_format', function( $format, $product ) {

    // Select the format 'upto' for the products in the shop loop
    if ( did_action( 'woocommerce_before_shop_loop_item' ) > did_action( 'woocommerce_after_shop_loop_item' ) ) {
        $format = 'upto';
    }

    // Select the default format if the product has more than 10 variations
    // (e.g. to avoid long lists when "List of all variations"
    // is selected as a global option
    if ( count( $product->get_children() ) > 10 ) {
        $format = 'default';
    }
    return $format;
}, 10, 2 );

wc_variation_prices_apply_single_price_format

Filter that allows you to override the default behavior with products that don’t have a price range (e.g. simple products). By design, Variation Prices for WooCommerce only affects the price range of variable or grouped products. When you select the Custom format, though, you are offered the possibility to define two custom formats: the first one is used when the variation or child products have different prices; the second one is used when all prices are the same. The customization of this second format might lead to a difference between single-priced variable or grouped products and all the other types of products. To solve this, you can use the default __return_true function as a callback attached to this filter. The plugin will then apply the single-price format to any type of non-variable product.

add_filter( 'wc_variation_prices_apply_single_price_format', '__return_true' );
Was this article helpful?

Related Articles