1

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.

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.

3

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
  • Do you know what !$product->min_variation_price means? Also, how the _x() function works? Nov 19 '13 at 23:53
  • It does mean not - ! - the products - $product - minimal variation price - min_variation_price; there's really not more to say. Most of the variation stuff is defined and handled by class-wc-product-variable.php if you want to take a look at the source. @RaphaelRafatpanah
    – Nicolai
    Nov 20 '13 at 0:02
1

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.

1
  • 1
    You don't need the conditional, if you're not doing something if the condition is met. Take a look at I18n for WordPress Developers, _x() for distinction by context.
    – Nicolai
    Nov 19 '13 at 23:55
0

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;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.