Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hello,
How can I get a list of available product template options via Shopify admin APIs (Rest or Graphql)?
In ProductInput fields (for product update/create mutation) we have a templateSuffix field to set, but I cannot find a way to get the list of all available product templates.
In Shopify admin, we have the block for it, that retrieves availableProductTemplateOptions array. I need the same but via admin API. Look at the screenshot:
Hello, did you find a solution for this?
Unfortunately, no
Someone just shared a solution with me. Apparently, you get them with the Assets API: https://shopify.dev/docs/api/admin-rest/2023-07/resources/asset#get-themes-theme-id-assets
But I don't know if this will actually work. I guess the template prefix would be part of the key value?
Hmm, it sounds like a possible solution, must try. Thank you!
I have the same issue. The asset API can get the assets of a theme. But there is no key filter. So I don't think it can retrieve all product templates. Getting all theme assets and filter the files may work though
Did you found a solution?
Big thanks to @DavidT who pointed me at asset resource.
My solution:
import { authenticate } from "../shopify.server";
// shopify.server contains shopifyApp object initialization:
// import { shopifyApp } from "@shopify/shopify-app-remix/server";
// ...
// const shopify = shopifyApp(...);
// export const authenticate = shopify.authenticate;
// then exported authenticate variable is called below to get access to Rest API
const {admin, session} = await authenticate.admin(request);
const shopifyRestApi = admin.rest.resources;
const assets = await shopifyRestApi.Asset.all({ session: session, theme_id: <theme_id> }); // for example, take theme_id from editor, there is number in URL after /themes
// now I have all asset data, hundreds of elements...
const templatePageKeyPrefix = 'templates/page.';
const templateDefaultPageKey = 'templates/page.json';
const templatePageKeys = assets.data.filter(asset => {
const templateKey = asset.key;
return templateKey.startsWith(templatePageKeyPrefix) && templateDefaultPageKey !== templateKey;
});
// now I have page templates without 'Default page' page template
// templatePage.key looks like this: templates/page.contact-page.json
// I need it to looks like this: contact-page
const templateSuffixes = templatePageKeys.map(templatePage => {
return templatePage.key.split('/')[1].split('.')[1];
});
// templateSuffixes contains array of page template suffixes, for example: [contact-page, about-us, terms-and-conditions]
`templateSuffixes` term comes from Page resouce: https://shopify.dev/docs/api/admin-rest/2024-01/resources/page#get-pages
`template_suffix` holds value which bounds static page with specific template page.
At first I used Page resource to get all templates from theme, but one template page could be used by multiple static pages, so I searched for better solution. The 'better solution' is this Asset resource.
Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024