1. Home
  2. Knowledge Base
  3. WooCommerce Fees and Discounts
  4. Advanced
  5. Add text to price label
  1. Home
  2. Knowledge Base
  3. WooCommerce Fees and Discounts
  4. Add text to price label

Add text to price label

Fees and Discounts will update the product price label to show a price range that includes possible discounted values.

If you’d like to change the text the label displays, you can use the following snippet. In this example, instead of displaying the price as a range, e.g. ‘$15 – $20’, the price will be displayed as ‘From $15’.

<?php
/**
* Filter the price html on shop and product pages
* @todo Display correct variation price range in variation.php
*/
function prefix_get_price_html( $price, $product ) {
$rules = get_option( 'wcfad_dynamic_pricing_rules', false );
// Get the best user role defined price
$price = $product->get_price();
$best_price = wcfad_get_product_price_by_role( $product, $price );
// Check for variation pricing
if( $product->is_type( 'variable' ) ) {
$prices = $product->get_variation_prices( true );
$min_variation_price = wcfad_get_product_price_by_role( $product, current( $prices['price'] ) );
$max_variation_price = wcfad_get_product_price_by_role( $product, end( $prices['price'] ) );
$min_max = array( $min_variation_price, $max_variation_price );
} else {
$min_max = array( $best_price, $best_price );
}
$qualifies = false;
$product_id = $product->get_id();
if( $rules ) {
foreach( $rules as $rule ) {
$validation_function = '';
// Iterate through each rule
if( ! empty( $rule['rule'] ) ) {
// Check if this product is covered by a pricing rule
$qualifies = false;
$label = isset( $rule['label'] ) ? $rule['label'] : '';
$applies_to = isset( $rule['applies_to'] ) ? $rule['applies_to'] : 'all';
// The list of products that qualify for the offer
if( $applies_to == 'all' ) {
$qualifies = true;
$min_max = wcfad_get_min_max_pricing( $min_max, $rule, $best_price );
// break;
} else if( $applies_to == 'products' ) {
$buy_products = isset( $rule['buy_products'] ) ? $rule['buy_products'] : 'all';
if( $buy_products == 'all' || ( is_array( $buy_products ) && in_array( $product_id, $buy_products ) ) ) {
$qualifies = true;
$min_max = wcfad_get_min_max_pricing( $min_max, $rule, $best_price );
// break;
}
} else if( $applies_to == 'categories' ) {
$buy_categories = isset( $rule['buy_categories'] ) ? $rule['buy_categories'] : 'all';
if( $buy_products == 'all' || ( is_array( $buy_products ) && has_term( $get_categories, 'product_cat', $product_id ) ) ) {
$qualifies = true;
$min_max = wcfad_get_min_max_pricing( $min_max, $rule, $best_price );
// break;
}
}
}
}
}
if( $min_max[0] != $min_max[1] ) {
// Display the formatted price range
$price_html = sprintf(
'<p class="price">From %s</p>',
wc_price( $min_max[0] )
);
} else {
$price_html = sprintf(
'<p class="price">From %s</p>',
wc_price( $best_price )
);
}
if( $qualifies ) {
$price_html = sprintf(
'%s<br><span class="wcfad-rule-label">%s</span>',
$price_html,
$label
);
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'prefix_get_price_html', 999, 2 );

You can add this snippet by following these steps.

Was this article helpful?

Related Articles