3

The elementApi docs state you can use custom elementTypes via plugins. I am using craft commerce which states it creates the custom elementType 'purchasable'

I've tried:

return [
'endpoints' => [
    'category.json' => [
        'elementType' => 'purchasable',
        'criteria' => [
            // 'category' => 'products', // randomly trying stuff here!
            // 'section' => 'products',
            // 'type' => 'products',
        ],
        'transformer' => function(EntryModel $entry) {
            return [
                'title' => $entry->title,
                'url' => $entry->url,
            ];
        },
    ]
]]

I've tried many variations on the word purchasable, as we as random guesses like 'product', 'item' etc etc

I should mention I am able to JSON back with all categories, Entries etc etc

I am sure this is possible, as craft, craftcommerce and elementApi are all made by the same guys!

Any help would be amazing

  • What's the class name of your purchasable? – Brad Bell Mar 23 '17 at 0:04
3

Try

return [
'endpoints' => [
    'products.json' => [
        'elementType' => 'Commerce_Product',
        'criteria' => [
            // 'category' => 'products', // randomly trying stuff here!
            // 'section' => 'products',
            // 'type' => 'products',
        ],
        'transformer' => function(Commerce_ProductModel $product) {
            return [
                'title' => $product->title,
                'url' => $product->url,
            ];
        },
    ]
]]
  • Thanks, but that's giving me the classic: No element type exists by the type “Commerce_Products”. – Chilly Mar 23 '17 at 0:16
  • Ah! drop the 's' ! : Commerce_Product thank you! – Chilly Mar 23 '17 at 0:17
  • If you are using Commerce 2, the Commerce_Product should be Product and add a use craft\commerce\elements\Product at the top of your file. – Luke Holder Oct 2 '18 at 9:33
1

For Commerce 2

use craft\commerce\elements\Product;
return [
    'endpoints' => [
    'products.json' => [
        'elementType' => Product::class,
        'transformer' => function(Product $product) {
            return [
                'title' => $product->title,
                'url' => $product->url,
            ];
        },
    ],
    ]
];

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.