0

I have a catalog of games. Every game has its own price. The price is a numeric field.

At the moment I'm building a game catalog filter, and now I'm stuck on implementing the ability to filter products by price. There are two input in the filter form: maximum and minimum price fields.

    <div>
        Search by price:<br />
        from:<input name="min" type="text" value="">
        to:<input name="max" type="text" value="">
    </div>

By filling in these two fields, the user needs to show the games within the selected price. A few questions, the answers to which will help me get closer to the solution:

1.How do I find out the price of the most expensive and minimal game?

2.This is how in the twig template I check for the presence of the minprice or maxprice parameter in the url.

{% set games = craft.entries().section('games') %}

{% set minprice = craft.app.request.getQueryParam('min') %}
{% if minprice %}
    {% do .................. %}
{% endif %}

{% set maxprice = craft.app.request.getQueryParam('max') %}
{% if maxprice %}
    {% do .................. %}
{% endif %}

I do not know what to write in my code instead of dots. Help me please with this))

1 Answer 1

2

The documentation for every field type includes examples for sorting and filtering by that field. Here's the documentation for the numbers field which includes examples for this exact scenario.

If there's only a minimum or a maximum value, you can filter the price with the >= or <= operator, respectively:

{% if min %}
    {% do games.price(">= #{min}") %}
{% endif %}

However, if there's both a minimum and a maximum value, you can't add both filters separately, because the second one will overwrite the first one. In this case, you need to use the array syntax as demonstrated in the documentation linked above:

{% if min and max %}
    {% do games.price(['AND', ">= #{min}", "<= #{max}"]) %}
{% endif %}

In both examples, replace price with the handle of your numeric price field.

How do I find out the price of the most expensive and minimal game?

To find the most and least expensive game, use an entry query and sort by price. Then get the first/last item from the list using .one() and take the price from that entry:

{% set leastExpensiveGame = craft.entries()
        .section('games')
        .orderBy('price ASC')
        .one()
%}
{% set minimumPrice = leastExpensiveGame.price %}

Replace price with the handle of your price field. For the most expensive game, use another query with the same parameters, but sort by price DESC (descending).

5
  • Thx for answer. But i still don't understand how to get price of the most expensive or minimal game from my catalog?
    – Dimi
    Nov 17, 2022 at 12:52
  • I came up with just such a solution: {% set max = craft.entries().section('games').orderBy('priceBaz DESC').one() %} But I do not know how much it is correct
    – Dimi
    Nov 17, 2022 at 13:52
  • @Dimi Ah, I missed that part of the question. I've updated my answer with some code to get the least/most expensive item.
    – MoritzLost
    Nov 18, 2022 at 16:14
  • Well, in principle, I thought that you can search for the maximum value through ORDER. However, I thought there was a solution of something like: set max = craft.entries().section('games').getValue('priceField').max()
    – Dimi
    Nov 18, 2022 at 17:45
  • @Dimi There's more than one way to skin a cat. Using two queries with ASC/DESC order is perfectly fine. If you want to further optimize the queries, use select() to limit the results to one particular field (see this question and all the answers for details) and execute the query with .scalar() to get the price as a single scalar value. But I wouldn't spend my time worrying about theoretical performance bottlenecks until you actually hit one.
    – MoritzLost
    Nov 19, 2022 at 18:19

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.