How can I display the lowest price per kilogram on product categories?

Hello

on categories we see the price per kilogram for example for a product. It says “from xy€/kg”. But it is not the cheapest kg-price which is shown, it shows the base price for the first product variant. How can I change this?

See screenshot of product in category, with a price from 11,25€/kg and a variant of the product itself where price is 10,31€/kg

In the template I found this file: product-unit-price.liquid
it says: From {{ product_variant.unit_price | money }}
this must be the one to change, but I dont know to what I should change it

To solve the problem I could just change the order of all variants and have the cheapest kg-price as the first variant, but this doesnt make sense, right :wink:

Thanks for helping!

I think I was wrong witht he template file

I think the correct file is product-card.liquid

{%- if current_variant.unit_price.size > 0 -%}
        - {% include 'product-unit-price', product_variant: current_variant, wrapper_class: "product-card__base-price" %}
        

        {%- else -%}                        
               
          {% if product.compare_at_price > product.price %}
            {% comment %}
              Product is on sale
            {% endcomment %}
            {% if product.price_varies %}
              {% assign sale_price = product.price | money_without_trailing_zeros %}
              {{ 'products.product.on_sale_from_html' | t: price: sale_price }}
            {% else %}
              {{ 'products.product.regular_price' | t }}
              ~~{{ product.compare_at_price | money_without_trailing_zeros }}~~

              {{ 'products.product.sale_price' | t }}
              {{ product.price | money_without_trailing_zeros }}
            {% endif %}

          {% else %}
            {% comment %}
              Not on sale, but could still have varying prices
            {% endcomment %}
            {% if product.price_varies %}
              {% assign price = product.price | money_without_trailing_zeros %}
              {{ 'products.product.from_text_html' | t: price: price }}
            {% else %}
              {{ 'products.product.regular_price' | t }}
              {{ product.price | money_without_trailing_zeros }}
            {% endif %}

          {% endif %}
          {%- unless product.price_varies -%}
            {%- if current_variant.unit_price_measurement -%}
              {% include 'product-unit-price', product_variant: current_variant, wrapper_class: "product-card__unit-price" %}
            {%- endif -%}
          {%- endunless -%}
        
        {%- endif -%}

at some part it just use the first variant (maybe “current_variant”?) but I want the cheapest one to be shown
hope somebody could help