Replace variable products price range with From: + lowest price in WooCommerce
Your code is a bit incomplete as the hook and the function are missing…
Here is the correct way to make it works for your variable products:
add_filter( 'woocommerce_get_price_html', 'change_variable_products_price_display', 10, 2 );
function change_variable_products_price_display( $price, $product ) {
// Only for variable products type
if( ! $product->is_type('variable') ) return $price;
$prices = $product->get_variation_prices( true );
if ( empty( $prices['price'] ) )
return apply_filters( 'woocommerce_variable_empty_price_html', '', $product );
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
$prefix_html = '<span class="price-prefix">' . __('Fra: ') . '</span>';
$prefix = $min_price !== $max_price ? $prefix_html : ''; // HERE the prefix
return apply_filters( 'woocommerce_variable_price_html', $prefix . wc_price( $min_price ) . $product->get_price_suffix(), $product );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
How can I remove price range for variable products with only one variation?
Updated (replaced incorrect $this
with $product
).
Try the following, that should remove the price range for variable products with one variation:
add_filter( 'woocommerce_variable_price_html', 'variable_products_with_one_variation', 10, 2 );
function variable_products_with_one_variation( $price_html, $product ) {
$prices = $product->get_variation_prices( true );
if( count($prices['price']) == 1 ) {
$active_price = end( $prices['price'] );
$reg_price = end( $prices['regular_price'] );
if ( $this->is_on_sale() ) {
$price = wc_format_sale_price( wc_price( $reg_price ), wc_price( $active_price ) );
} else {
$price = wc_price( $active_price );
}
}
return $price;
}
It should work. Anyways if it doesn't work, this is the right hook to be used.
Related Topics
Codeigniter: Best Way to Structure Partial Views
Laravel No Supported Encrypter Found. the Cipher And/Or Key Length Are Invalid
Symfony2 Routing - Route Subdomains
How to Print SQL Statement in Codeigniter Model
Mysqli_Real_Connect(): (Hy000/2002): No Such File or Directory
S3 Direct Upload Restricting File Size and Type
How to Select All Column Name from a Table in Laravel
How to Set an Arrays Internal Pointer to a Specific Position? PHP/Xml
What Are the Valid Characters in PHP Variable, Method, Class, etc Names
Sending Multiple Data Parameters with Jquery Ajax
Mock in PHPunit - Multiple Configuration of the Same Method with Different Arguments
Why Are PHP Function Calls *So* Expensive
Codeigniter Multiple File Upload Messes File Extension
Adding Three Months to a Date in PHP
Stop Script Execution Upon Notice/Warning