<p>If you want to get a specific product and it's variants by the <code>productId</code> you could simply do:</p>

<pre class="lang-php prettyprint-override"><code>'api/product/&lt;productId&gt;' =&gt; function( $productId ) {
    return [
        'elementType' =&gt; Product::class,
        'criteria'    =&gt; [
            'id' =&gt; $productId
        ],
        'pretty'      =&gt; true,
        'transformer' =&gt; function( Product $product ) {
            foreach ( $product-&gt;getVariants() as $variant ) {
                $variants = [
                    'title'         =&gt; $variant-&gt;title,
                    'description'   =&gt; $variant-&gt;description,
                    'productId'     =&gt; (int) $variant-&gt;productId,
                    'purchasableId' =&gt; (int) $variant-&gt;purchasableId,
                    'status'        =&gt; $variant-&gt;status,
                    'type'          =&gt; $product-&gt;type-&gt;name,
                    'url'           =&gt; $product-&gt;url,
                ];
            }

            return $variants;
        },
    ];
},
</code></pre>

<p>you are able to get the product then via <code>yourshop.domain/product/1234</code></p>

CC BY-SA 4.0
2019-05-10T14:12:01.580Z
2019-05-10T14:12:01.580Z
HendrikEng
<p>How do I get a specific product via <code>Element API</code>?</p>

<p>Here's what i have so far</p>

<pre class="lang-php prettyprint-override"><code>'api/product' =&gt; function() {
    $productId = craft()-&gt;request-&gt;getRequiredQuery( 'productId' );

    return [
        'elementType' =&gt; Product::class,
        // This provides Element API the fully qualified Element namespace + class.
        'pretty'      =&gt; true,
        'criteria'    =&gt; [
            'relatedTo' =&gt; [ 'targetElement' =&gt; $categoryIds ],
        ],
        'transformer' =&gt; function( Product $product ) {
            return [
                'title' =&gt; $product-&gt;title,
                'url'   =&gt; $product-&gt;url
            ];
        }

    ];
}   
</code></pre>

<plugin-craftcommerce><plugin-elementapi><craft2>
Get a specific product with Element API, Commerce and Craft 2
# Get a specific product with Element API, Commerce and Craft 2

(Asked by: username_0 on 2019-05-10)

How do I get a specific product via Element API?
Here's what i have so far
'api/product' => function() {
    $productId = craft()->request->getRequiredQuery( 'productId' );

    return [
        'elementType' => Product::class,
        // This provides Element API the fully qualified Element namespace + class.
        'pretty'      => true,
        'criteria'    => [
            'relatedTo' => [ 'targetElement' => $categoryIds ],
        ],
        'transformer' => function( Product $product ) {
            return [
                'title' => $product->title,
                'url'   => $product->url
            ];
        }

    ];
}

**Tags:** plugin-craftcommerce, plugin-elementapi, craft2


---

## 1 answers

### Answer by username_1 on 2019-05-10. Score: 1

If you want to get a specific product and it's variants by the productId you could simply do:
'api/product/' => function( $productId ) {
    return [
        'elementType' => Product::class,
        'criteria'    => [
            'id' => $productId
        ],
        'pretty'      => true,
        'transformer' => function( Product $product ) {
            foreach ( $product->getVariants() as $variant ) {
                $variants = [
                    'title'         => $variant->title,
                    'description'   => $variant->description,
                    'productId'     => (int) $variant->productId,
                    'purchasableId' => (int) $variant->purchasableId,
                    'status'        => $variant->status,
                    'type'          => $product->type->name,
                    'url'           => $product->url,
                ];
            }

            return $variants;
        },
    ];
},

you are able to get the product then via yourshop.domain/product/1234


---

