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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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