Replace the Variable Price Range by the Chosen Variation Price in Woocommerce 3

Replace the Variable Price range by the chosen variation price in WooCommerce 4+

Update March 2021 (Works at least from WooCommerce 3.7 up to 5+)

The code:

add_action('woocommerce_before_add_to_cart_form', 'selected_variation_price_replace_variable_price_range');
function selected_variation_price_replace_variable_price_range(){
global $product;

if( $product->is_type('variable') ):
?><style> .woocommerce-variation-price {display:none;} </style>
<script>
jQuery(function($) {
var p = 'p.price'
q = $(p).html();

$('form.cart').on('show_variation', function( event, data ) {
if ( data.price_html ) {
$(p).html(data.price_html);
}
}).on('hide_variation', function( event ) {
$(p).html(q);
});
});
</script>
<?php
endif;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

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.



Related Topics



Leave a reply



Submit