WooCommerce Variable Product Price – Where is “From” text generated from?

The following code from github will add “- [max price]” to all variable products displayed in product archives.

I am wondering how I could also remove the “From” text inside the same filter.

Read More
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);

function custom_variation_price( $price, $product ) {

     $price = '';

     if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price ) $price .= '<span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';

     $price .= woocommerce_price($product->get_price());

     if ( $product->max_variation_price && $product->max_variation_price !== $product->min_variation_price ) {
          $price .= '<span class="to"> ' . _x('-', 'max_price', 'woocommerce') . ' </span>';

          $price .= woocommerce_price($product->max_variation_price);
     }

     return $price;
}

I noticed this question answers how to remove the From text using the same filter parameters, but I haven’t been able to figure out how this works.

Related posts

3 comments

  1. Go with commenting out this line:

     if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price ) $price .= '<span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';
    

    and this won’t be added to the price output.

  2. Well, it turns out I have done it but without really exactly how everything works.

    add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
    
    function custom_variation_price( $price, $product ) {
    
         $price = '';
    
         if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price ) $price .= '<span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';
    
         $price .= woocommerce_price($product->get_price());
    
         if ( $product->max_variation_price && $product->max_variation_price !== $product->min_variation_price ) {
              $price .= '<span class="to"> ' . _x('-', 'max_price', 'woocommerce') . ' </span>';
    
              $price .= woocommerce_price($product->max_variation_price);
         }
    
         return $price;
    }
    

    I just change this line:

    if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price ) $price .= '<span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';
    

    to:

    if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price )
    

    Does anyone know exactly how the _x() function works? I did see their documentation but its not really descriptive.

  3. Use this code to remove “FROM” text from the variable products:

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

Comments are closed.