<p><strong>The Simplest Answer</strong></p>

<p><code>{% set products = craft.commerce.products.search(query).order('score') %}</code></p>

<p>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.</p>

<p><strong>A Slightly Fuller Answer</strong></p>

<p>(Using your form)</p>

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

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

<p>(If you want to search <em>within</em> those products for something else, <em>then</em> you need the <code>search: "some value"</code> part).</p>

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

<pre><code>{% 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...
</code></pre>

<p>(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)</p>

<hr>

<p><strong>A Considerably Fuller Answer</strong></p>

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

<p>Here's some sample code that will get you started:</p>

<pre><code>    {% 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 %}

        &lt;br&gt;&lt;br&gt;

        {% 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
</code></pre>

CC BY-SA 3.0
2016-05-11T15:17:39.350Z
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
CC BY-SA 3.0
2016-05-11T21:08:49.560Z
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
CC BY-SA 3.0
2016-05-11T23:35:25.790Z
See updated answer...
Jeremy Daalder
CC BY-SA 3.0
2016-05-12T21:01:38.590Z
This did it....
{% set products = craft.commerce.products.search(query).order('score') %}
user1663102
CC BY-SA 3.0
2016-05-12T23:20:35.310Z
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
CC BY-SA 3.0
2016-05-05T23:20:00.813Z
2016-05-12T23:18:53.550Z
2016-05-12T23:18:53.550Z
Jeremy Daalder
<p>I am using the following form in Craft</p>

<pre><code>&lt;div class="parts-search"&gt;

    &lt;form class="parts-search-block" action="{{ url('parts/results') }}" method="get"&gt;

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

        &lt;div class="col col-34"&gt;
            &lt;button type="submit" class="cat-search"&gt;
                Begin Search
            &lt;/button&gt;
        &lt;/div&gt;

        &lt;div class="clear-fix"&gt;&lt;/div&gt;
    &lt;/form&gt;
</code></pre>

<p>And calling it on the results page with...</p>

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

    {% for entry in entries %}
</code></pre>

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

CC BY-SA 3.0
2016-08-11T10:25:09.167Z
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
CC BY-SA 3.0
2016-08-11T17:00:19.017Z
@JeremyDaalder: I've reverted the edit back to the actual question.
Lindsey D
CC BY-SA 3.0
2016-08-11T17:01:35.103Z
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
<plugin-craftcommerce>
Search Form with Craft Commerce
# Search Form with Craft Commerce

(Asked by: username_0 on 2016-05-05)

I am using the following form in Craft


    

        {#-- CATEGORY SEARCH --#}
        
            Search by Category:
            
                --Select A Category--
                Compressors
                Coolers
                Doors
                Electrical
                Freezers
                Gaskets
                GFV
                Glass
                Graphics
                Grills
                Harnesses
                Lighting
                Misc
                Motors
                Plastic
                Refrigeration
                Repair Panels
                Shelves
                Skins
                Vendor
            
        
        {#-- CATEGORY SEARCH --#}

        
            
                Begin Search
            
        

        
    

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?

**Tags:** plugin-craftcommerce

> **Comments:**
> **username_1**: You should not delete the original question - otherwise the answers have no context.  Stack exchange operates best as a question and answer forum.  
> **username_2**: @JeremyDaalder: I've reverted the edit back to the actual question.  
> **username_2**: 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.  

---

## 1 answers

### Answer by username_1 on 2016-05-05. Score: 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 %}

        

        {% 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

> **Comments:**
> **username_0**: 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.  
> **username_1**: 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!)  
> **username_1**: See updated answer...  
> **username_0**: This did it.... {% set products = craft.commerce.products.search(query).order('score') %}  
> **username_1**: 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!)  

---

