<p>Products can be queried by the product types <code>type</code> (a handle or ProductTypeModel) or <code>typeId</code> the ID of the type.</p>

<p>In a template this would look like:</p>

<pre><code>craft.commerce.products.typeId([1,3,4]).find()
</code></pre>

<p>or</p>

<pre><code>craft.commerce.products.type(['new','another']).find()
</code></pre>

<p>This is as per the parameter value syntax: <a href="https://craftcms.com/docs/templating/elementcriteriamodel#parameter-value-syntax" rel="nofollow noreferrer">https://craftcms.com/docs/templating/elementcriteriamodel#parameter-value-syntax</a></p>

<p>So in an element criteria query syntax in PHP it would look like this:</p>

<pre><code>'criteria' =&gt; [
  'type' =&gt; ['New','Another']
],
</code></pre>

<p>or</p>

<pre><code>'criteria' =&gt; [
  'typeId' =&gt; [1,2]
],
</code></pre>

<p>or</p>

<pre><code>'criteria' =&gt; [
  'type' =&gt; craft()-&gt;commerce_productTypes-&gt;getAllProductTypes()[0]
],
</code></pre>

<p>The example you can was to build a query string for the search element criteria param, which could optionally be another criteria if you wanted.</p>

CC BY-SA 3.0
2017-09-26T18:58:47.863Z
Hi Luke, thanks for the detailed explanation. I just need a little clarity about using my search criteria for the url.

If the structure of the url above that I showed was used in my search criteria how could we build the queries in? Would I add it into the function? `buildSearchQuery()`
M dunbavan
CC BY-SA 3.0
2017-09-26T05:36:05.907Z
2017-09-26T05:36:05.907Z
Luke Holder
<p>I am building an API that takes all products from the Craft Commerce <code>Commerce_ProductModel</code>. In this I am using the Element API plugin to build it out. I am running on Craft 2 latest version.</p>

<p>I have so far built out the JSON API and it's returning what I need with the following code:</p>

<pre><code>'api/allproducts' =&gt; [
    'elementType' =&gt; 'Commerce_Product',
    'criteria' =&gt; [
        'search' =&gt; buildSearchQuery(craft()-&gt;request-&gt;getParam('types'))
    ],
    'transformer' =&gt; function(Commerce_ProductModel $product) {

        $mergedProductData = [];

        $title = $product-&gt;title;
        $url = UrlHelper::getUrl($product-&gt;url);
        $brand = (string)$product-&gt;productBrand[0];
        $designer = (string)$product-&gt;productDesigner[0];
        $num_decimals = (intval($product-&gt;defaultVariant-&gt;price) == $product-&gt;defaultVariant-&gt;price) ? 0 :2;
        $price = number_format($product-&gt;defaultVariant-&gt;price,$num_decimals);

        $frontImage = $product-&gt;productImages[0];
        $frontImages = [];

        if ($frontImage)
        {
            craft()-&gt;config-&gt;set('generateTransformsBeforePageLoad', true);

            if (craft()-&gt;plugins-&gt;getPlugin('Imager'))
            {
                $transformedImages = craft()-&gt;imager-&gt;transformImage(
                    $frontImage,
                    array(
                        array(
                            'width' =&gt; 1170,
                        ),
                        array(
                            'width' =&gt; 780,
                            'jpegQuality' =&gt; 65,
                        ),
                        array(
                            'width' =&gt; 390,
                            'jpegQuality' =&gt; 65,
                        ),
                    ),
                    array(
                        'ratio' =&gt; 16/9,
                        'position' =&gt; 'bottom-right',
                        'jpegQuality' =&gt; 80,
                    ),
                    array(
                        'fillTransforms' =&gt; true,
                    )
                );

                $frontImages[] = craft()-&gt;imager-&gt;srcset($transformedImages);

            } else {
                $frontImages[] = $frontImage-&gt;getUrl(['width' =&gt; 390, 'height' =&gt; 294]);
            }
        }

        $mergedProductData[] = [
            'id' =&gt; $product-&gt;id,
            'title' =&gt; $title,
            'productImage' =&gt; $frontImages,
            'body' =&gt; (string) $product-&gt;body,
            'brand' =&gt; $brand,
            'designer' =&gt; $designer,
            'url' =&gt; $url
        ];

        return [
            'title' =&gt; $title,
            'id' =&gt; $product-&gt;id,
            'frontImage' =&gt; $frontImages,
            'type' =&gt; $product-&gt;type-&gt;name,
            'designer' =&gt; $designer,
            'brand' =&gt; $brand,
            'price' =&gt; $price,
            'url' =&gt; $url,
            'isInWishlist' =&gt; craft()-&gt;like-&gt;isLike($product-&gt;id),
            'quickViewJson' =&gt; $mergedProductData
        ];
    },
],
</code></pre>

<p>This is great, its returning what I need and its nothing more but I am trying to return search values against it by looking for the product type form the <code>Commerce_ProductModel</code> and its hard to figure out.</p>

<p>What I ideally want to do is run a URL query like so:</p>

<p><code>/api/allproducts?types[]=New&amp;types[]=Tables</code></p>

<p>In the code above there is this function that I was given by another developer that allows you to build queries like the URL above.</p>

<p><code>buildSearchQuery(craft()-&gt;request-&gt;getParam('types'))</code> </p>

<p>In this I want to be able to query any <code>product type</code> from Craft Commerce so it only returns products of these types. The function is below:</p>

<pre><code>function buildSearchQuery($input) {

    $query = "";
    $i = 0;

    foreach ($input as $value) {

        if($i &gt; 0) {
            $query .= " OR ";
        }

        $query .= "title:*$value* OR body:*$value*";
        $i++;
    }

    return $query;
}
</code></pre>

<p>Can anyone shed any light on how this can be done at all?</p>

<plugin-craftcommerce><ecommerce><query><querystring>
Is it possible to search in Craft Commerce by product type?
# Is it possible to search in Craft Commerce by product type?

(Asked by: username_0 on 2017-09-25)

I am building an API that takes all products from the Craft Commerce Commerce_ProductModel. In this I am using the Element API plugin to build it out. I am running on Craft 2 latest version.
I have so far built out the JSON API and it's returning what I need with the following code:
'api/allproducts' => [
    'elementType' => 'Commerce_Product',
    'criteria' => [
        'search' => buildSearchQuery(craft()->request->getParam('types'))
    ],
    'transformer' => function(Commerce_ProductModel $product) {

        $mergedProductData = [];

        $title = $product->title;
        $url = UrlHelper::getUrl($product->url);
        $brand = (string)$product->productBrand[0];
        $designer = (string)$product->productDesigner[0];
        $num_decimals = (intval($product->defaultVariant->price) == $product->defaultVariant->price) ? 0 :2;
        $price = number_format($product->defaultVariant->price,$num_decimals);

        $frontImage = $product->productImages[0];
        $frontImages = [];

        if ($frontImage)
        {
            craft()->config->set('generateTransformsBeforePageLoad', true);

            if (craft()->plugins->getPlugin('Imager'))
            {
                $transformedImages = craft()->imager->transformImage(
                    $frontImage,
                    array(
                        array(
                            'width' => 1170,
                        ),
                        array(
                            'width' => 780,
                            'jpegQuality' => 65,
                        ),
                        array(
                            'width' => 390,
                            'jpegQuality' => 65,
                        ),
                    ),
                    array(
                        'ratio' => 16/9,
                        'position' => 'bottom-right',
                        'jpegQuality' => 80,
                    ),
                    array(
                        'fillTransforms' => true,
                    )
                );

                $frontImages[] = craft()->imager->srcset($transformedImages);

            } else {
                $frontImages[] = $frontImage->getUrl(['width' => 390, 'height' => 294]);
            }
        }

        $mergedProductData[] = [
            'id' => $product->id,
            'title' => $title,
            'productImage' => $frontImages,
            'body' => (string) $product->body,
            'brand' => $brand,
            'designer' => $designer,
            'url' => $url
        ];

        return [
            'title' => $title,
            'id' => $product->id,
            'frontImage' => $frontImages,
            'type' => $product->type->name,
            'designer' => $designer,
            'brand' => $brand,
            'price' => $price,
            'url' => $url,
            'isInWishlist' => craft()->like->isLike($product->id),
            'quickViewJson' => $mergedProductData
        ];
    },
],

This is great, its returning what I need and its nothing more but I am trying to return search values against it by looking for the product type form the Commerce_ProductModel and its hard to figure out.
What I ideally want to do is run a URL query like so:
/api/allproducts?types[]=New&types[]=Tables
In the code above there is this function that I was given by another developer that allows you to build queries like the URL above.
buildSearchQuery(craft()->request->getParam('types')) 
In this I want to be able to query any product type from Craft Commerce so it only returns products of these types. The function is below:
function buildSearchQuery($input) {

    $query = "";
    $i = 0;

    foreach ($input as $value) {

        if($i > 0) {
            $query .= " OR ";
        }

        $query .= "title:*$value* OR body:*$value*";
        $i++;
    }

    return $query;
}

Can anyone shed any light on how this can be done at all?

**Tags:** plugin-craftcommerce, ecommerce, query, querystring


---

## 1 answers

### Answer by username_1 on 2017-09-26. Score: 1

Products can be queried by the product types type (a handle or ProductTypeModel) or typeId the ID of the type.
In a template this would look like:
craft.commerce.products.typeId([1,3,4]).find()

or
craft.commerce.products.type(['new','another']).find()

This is as per the parameter value syntax: https://craftcms.com/docs/templating/elementcriteriamodel#parameter-value-syntax
So in an element criteria query syntax in PHP it would look like this:
'criteria' => [
  'type' => ['New','Another']
],

or
'criteria' => [
  'typeId' => [1,2]
],

or
'criteria' => [
  'type' => craft()->commerce_productTypes->getAllProductTypes()[0]
],

The example you can was to build a query string for the search element criteria param, which could optionally be another criteria if you wanted.

> **Comments:**
> **username_0**: Hi Luke, thanks for the detailed explanation. I just need a little clarity about using my search criteria for the url.  If the structure of the url above that I showed was used in my search criteria how could we build the queries in? Would I add it into the function? `buildSearchQuery()`  

---

