I got Drupal 8 / Commerce 2 installation, and trying to create product category pages, showing product image, title and price range (min & max price)

I've created product category page with Views. Category is defined as Taxonomy reference field in product. Views has "Contextual filter", where category name is taken from url e.g. /product/[category].

Product catalog page works fine, showing fields defined in (custom) Summary view mode. It shows title and the first product variation image and price.

Only thing missing is the price range info

I've tried to do some experiments by

  • overwriting commerce-product.html.twig in custom theme

  • writing hook__preprocess_commerce_product in custom module

..but with no luck. Debugging is hard because, for some reason kint() / dpm() calls fail or just take tooo long and give execution time out...

So the question is: how and where to check product variations lowest & highest prices plus pass them to product template?

Any hints or ideas are greatly appreciated.


I got it done by adding the following preprocess hook to my custom module

function CUSTOMMODULE_preprocess_commerce_product(&$variables) {

  $product = $variables['elements']['#commerce_product'];

  foreach( $product->variations as $variation) {
    $prices[] = $variation->entity->getPrice()->getNumber();

  $min_price = min($prices);
  $max_price = max($prices);

  if ($min_price != $max_price) {
    $variables['min_price'] = $min_price;
    $variables['max_price'] = $max_price;

  $variables['currency'] = $variation->entity->getPrice()->getCurrencyCode();

... And then in commerce-product--teaser.html.twig

{% if min_price is defined and max_price is defined %}
  {{ min_price |number_format(2, '.', ',') }} -
  {{ max_price |number_format(2, '.', ',') }} {{ currency }}
{% else %}
  {{ product.price }}
{% endif %}

1 Answer 1


I don't think the min max would be readily available in the template and you'll have to add it in.

You could use hook_preprocess_commerce_product($variables) and within $variables it will contain the product entity and also all of its variations. You would just need to loop through the variations to work out the min and max price and then add the new value into $variables e.g.

$variables['min_max_price'] = $myvalue;

Then in your template commerce-product.html.twig do:

{{ min_max_price }}

and that should come out with your $myvalue value

  • 1
    thank you so much!! This lead me to the right track, and I managed to get this done with preprocess hook. Now just need to get it work with taxes etc. and find another way of debugging...since for some reason kint is not working very well with my installation :(
    – SPR
    Oct 26, 2018 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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