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

1
  • What's the class name of your purchasable?
    – Brad Bell
    Commented Mar 23, 2017 at 0:04

2 Answers 2

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,
            ];
        },
    ]
]]
3
  • Thanks, but that's giving me the classic: No element type exists by the type “Commerce_Products”.
    – Chilly
    Commented Mar 23, 2017 at 0:16
  • Ah! drop the 's' ! : Commerce_Product thank you!
    – Chilly
    Commented Mar 23, 2017 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. Commented Oct 2, 2018 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 and acknowledge you have read our privacy policy.

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