Home > front end >  woocommerce_variable_price_html not able to set custom from price or text
woocommerce_variable_price_html not able to set custom from price or text

Time:09-30

I wish to only display the from price on my variable products.

So far it feels like I've tried everything, but maybe I'm missing something... I can get it to say "From:", but cannot get it to say "test" or "Starting price:"

The code that works partially for me:

add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
 
    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
    
    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
    
    if ( $price !== $saleprice ) {
        $price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
    }
    return $price;
}

I would like it to just say anything I wish.

add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
    return "test";
}

CodePudding user response:

The following code worked for me:

add_filter( 'woocommerce_variable_price_html', 'custom_variable_price_format', 1, 2 );
function custom_variable_price_format( $price, $product ) {
    $prices = $product->get_variation_prices( true );
    $min_price = current( $prices['price'] );
    $price = sprintf('Fra %1$s', wc_price( $min_price ) );
    return $price;
}

Also I had issues with the WooCommerce not displaying my changes, this was solved by going to another product or going to the next page of products.

CodePudding user response:

Please try this code -

add_filter( 'woocommerce_get_price_html', 'add_text_before_product_price' );

function add_text_before_product_price( $price ) {
    // Your additional text in a translatable string
    $text = __('FROM');

    // returning the text before the price
    return $text . ' ' . $price;
}
  • Related