<p>There are some issues with your PHP syntax – I didn't test this (not sure how the data model returned by <code>craft()-&gt;shopify-&gt;getProductById()</code> looks, and you'll need to verify that <code>$entry-&gt;shopifyProduct</code> returns a Shopify product ID), but at least the syntax errors should be fixed:</p>

<pre><code>return [
    'endpoints' =&gt; [
        'api.json' =&gt; [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; [
                'section' =&gt; 'product',
            ],
            'transformer' =&gt; function (EntryModel $entry) {
                $shopify = craft()-&gt;shopify-&gt;getProductById([ 'id' =&gt; $entry-&gt;shopifyProduct ]);
                return [
                    'id' =&gt; $entry-&gt;id,
                    'title' =&gt; $entry-&gt;title,
                    'url' =&gt; $entry-&gt;url,
                    'shopifyId' =&gt; $shopify ? $shopify-&gt;id : null,
                ];
            },
        ]
    ]
];
</code></pre>

CC BY-SA 4.0
2018-08-03T10:55:25.767Z
2018-08-03T10:55:25.767Z
Mats Mikkel Rummelhoff
<p>With version 3.0 of the Shopify plugin (designed for Craft 4), products are now synchronized into Craft as elements, so you can make them available via Element API (with the native Shopify data <em>and</em> any custom fields you’ve added in Craft)!</p>
<p>We’ve provided <a href="https://github.com/craftcms/shopify#element-api" rel="nofollow noreferrer">an example</a> of working with the Element API in the documentation.</p>

CC BY-SA 4.0
2022-11-09T18:44:28.317Z
2022-11-09T18:44:28.317Z
August Miller
<p>Is there any way to translate this:</p>

<pre><code>{% set shopify = craft.shopify.getProductById({ id: entry.shopifyProduct, fields: 'variants, id' }) %}
</code></pre>

<p>to the Element API? </p>

<p>I've tried this, but obviously it doesn't work:</p>

<pre><code>return [
  'endpoints' =&gt; [
  'api.json' =&gt; [
  'elementType' =&gt; 'Entry',
  'criteria' =&gt; [
    'section' =&gt; product,
  ],
  'transformer' =&gt; function (EntryModel $entry) {
    $shopify = craft()-&gt;shopify-&gt;getProductById({id:$entry-&gt;shopifyProduct});

    return [
      'id' =&gt; $entry-&gt;id,
      'title' =&gt; $entry-&gt;title,
      'url' =&gt; $entry-&gt;url,
      'shopifyId' =&gt; $entry-&gt;$shopify
    ];
  },
]
 ]
];
</code></pre>

<plugins><plugin-elementapi>
Element API and Shopify plugin
# Element API and Shopify plugin

(Asked by: username_0 on 2018-08-03)

Is there any way to translate this:
{% set shopify = craft.shopify.getProductById({ id: entry.shopifyProduct, fields: 'variants, id' }) %}

to the Element API? 
I've tried this, but obviously it doesn't work:
return [
  'endpoints' => [
  'api.json' => [
  'elementType' => 'Entry',
  'criteria' => [
    'section' => product,
  ],
  'transformer' => function (EntryModel $entry) {
    $shopify = craft()->shopify->getProductById({id:$entry->shopifyProduct});

    return [
      'id' => $entry->id,
      'title' => $entry->title,
      'url' => $entry->url,
      'shopifyId' => $entry->$shopify
    ];
  },
]
 ]
];

**Tags:** plugins, plugin-elementapi

---

## 2 answers

### Answer by username_1 on 2018-08-03. Score: 1 | Accepted answer

There are some issues with your PHP syntax – I didn't test this (not sure how the data model returned by craft()->shopify->getProductById() looks, and you'll need to verify that $entry->shopifyProduct returns a Shopify product ID), but at least the syntax errors should be fixed:
return [
    'endpoints' => [
        'api.json' => [
            'elementType' => 'Entry',
            'criteria' => [
                'section' => 'product',
            ],
            'transformer' => function (EntryModel $entry) {
                $shopify = craft()->shopify->getProductById([ 'id' => $entry->shopifyProduct ]);
                return [
                    'id' => $entry->id,
                    'title' => $entry->title,
                    'url' => $entry->url,
                    'shopifyId' => $shopify ? $shopify->id : null,
                ];
            },
        ]
    ]
];


---

### Answer by username_2 on 2022-11-09. Score: 2

With version 3.0 of the Shopify plugin (designed for Craft 4), products are now synchronized into Craft as elements, so you can make them available via Element API (with the native Shopify data *and* any custom fields you’ve added in Craft)!

We’ve provided [an example](https://github.com/craftcms/shopify#element-api) of working with the Element API in the documentation.


---

