1

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
    ];
  },
]
 ]
];

2 Answers 2

1

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,
                ];
            },
        ]
    ]
];
0
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 of working with the Element API in the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.