Reputation: 635
I have a furniture site built on wordpress and woocommerce. For consistency, I wanted to have variations for all bedframes, even if they only come in one size (king, queen, twin). However, variation products on woocommerce naturally have a price range describing the minimum price to maximum price between variations. If I only have one variation, it has a blank minimum price, and still has the hyphen and then the maximum price, as shown below, making it seem like it's negative. How can I remove the hyphen for products with one variation (including where all the other variations are disabled).
This site is khemlanimart.com
EDIT: I dont want it to just replace with the min price because when there is one variation, the min price is empty. If anything I wanted to it to be replaced with the max price in the case where there is only one variation and still keep the price range when there is multiple variations
Upvotes: 0
Views: 2386
Reputation: 253784
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.
Upvotes: 3