1

I am using the following form in Craft

<div class="parts-search">

    <form class="parts-search-block" action="{{ url('parts/results') }}" method="get">

        {#-- CATEGORY SEARCH --#}
        <div class="col col-33">
            <label class="form-top">Search by Category:</label><br />
            <select name="q">
                <option value="">--Select A Category--</option>
                <option value="Compressors">Compressors</option>
                <option value="Coolers">Coolers</option>
                <option value="Doors">Doors</option>
                <option value="Electrical">Electrical</option>
                <option value="Freezers">Freezers</option>
                <option value="Gaskets">Gaskets</option>
                <option value="GFV">GFV</option>
                <option value="Glass">Glass</option>
                <option value="Graphics">Graphics</option>
                <option value="Grills">Grills</option>
                <option value="Harnesses">Harnesses</option>
                <option value="Lighting">Lighting</option>
                <option value="Misc">Misc</option>
                <option value="Motors">Motors</option>
                <option value="Plastic">Plastic</option>
                <option value="Refrigeration">Refrigeration</option>
                <option value="Repair Panels">Repair Panels</option>
                <option value="Shelves">Shelves</option>
                <option value="Skins">Skins</option>
                <option value="Vendor">Vendor</option>
            </select>
        </div>
        {#-- CATEGORY SEARCH --#}

        <div class="col col-34">
            <button type="submit" class="cat-search">
                Begin Search
            </button>
        </div>

        <div class="clear-fix"></div>
    </form>

And calling it on the results page with...

    {% set query = craft.request.getParam('q') %}
    {% set entries = craft.entries.section('parts').search(query).order('score') %}

    {% for entry in entries %}

I am now using Craft Commerce and need to use the form to search my Commerce Products. Is this possible? Any suggestions?

  • You should not delete the original question - otherwise the answers have no context. Stack exchange operates best as a question and answer forum. – Jeremy Daalder Aug 11 '16 at 10:25
  • @JeremyDaalder: I've reverted the edit back to the actual question. – Lindsey D Aug 11 '16 at 17:00
  • To the OP... If you have found a solution on your own, please post it as an answer. Don't overwrite the question with your resolved code, that is not useful to others. – Lindsey D Aug 11 '16 at 17:01
2

The Simplest Answer

{% set products = craft.commerce.products.search(query).order('score') %}

However, I am not sure a search is guaranteed to return ALL the products in quite the same way as using relatedTo, which is how you'd typically get entries/products related to a category - so see below.

A Slightly Fuller Answer

(Using your form)

So in this case I think you're just wanting to get all products related to a category? As in, you're not actually searching within those right?

If so you can leave out the search parameter below, as all you're doing is finding the products related to the category.

(If you want to search within those products for something else, then you need the search: "some value" part).

Anyway, q has your category (title?) in it (would be better to use the slug in your form values really), so you can just:

{% set categoryTitle= craft.request.getParam('q') %}
{% set category = craft.categories.title(categoryTitle) %}  {# (or better change form values and this to .slug) #}
{% set products = craft.commerce.products({
                relatedTo: category,
                search: "whatever",  
                order: "title asc",
            }) %}
{% for product in products %}
    etc...

(Obviously a more typical way of doing this is to do it off a url segment or whatever for an index page, rather than by a search)


A Considerably Fuller Answer

Absolutely you can search products in the same way. Even better, since about Craft 2.5 when they added the serachScore to the results, you can simultaneously search multiple element types and combine the results.

Here's some sample code that will get you started:

    {% if query|length %}

    {% set product_results = craft.commerce.products.search(query).order('score').limit(limit_for_each_section).with('variants') %}  
    {% set article_results = craft.entries.search(query).order('score').limit(limit_for_each_section) %}
    {% set category_results = craft.categories.search(query).order('score').limit(limit_for_each_section) %}

    {% set all_results = [] %}

    {% if product_results|length %}
        {% set all_results = all_results|merge(product_results) %}
    {% endif %}
    {% if article_results|length %}
        {% set all_results = all_results|merge(article_results) %}
    {% endif %}
    {% if category_results|length %}
        {% set all_results = all_results|merge(category_results) %}
    {% endif %}

    {% if all_results|length %}

        <br><br>

        {% set all_results = all_results|sortByField('searchScore', 'desc') %}

        {% set items = [] %}

            {% for result in all_results %}

            {% if result.url|length %}

                {% if result.elementType == 'Commerce_Product' %}

                     ... etc
  • Jeremy, thank you for the help above. I have been trying to get it to work, but to no avail. Could you give me something a little more simply that works with just the form. Thank you in advance. – user1663102 May 11 '16 at 15:17
  • Post where you have got to (form plus results code) and I will take a look. Do an update to your post above (or come to the commerce slack!) – Jeremy Daalder May 11 '16 at 21:08
  • See updated answer... – Jeremy Daalder May 11 '16 at 23:35
  • This did it.... {% set products = craft.commerce.products.search(query).order('score') %} – user1663102 May 12 '16 at 21:01
  • Yes, that works, but it isn't really how you should get entries/products related to a category I think - it's possible it might not return ALL products in a category the way relatedTo will as it's doing a text style search, so you're probably better off using the approach detailed above (I've expanded it to cover three ways!) – Jeremy Daalder May 12 '16 at 23:20

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.