Re: Shopify admin API, get product theme templates

Shopify admin API, get product theme templates

BohdanNikitchuk
Shopify Partner
5 0 2

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:

BohdanNikitchuk_0-1691496716299.png

 

Replies 7 (7)

DavidT
Shopify Partner
38 2 15

Hello, did you find a solution for this?

QuickEdit - Bulk Product Edit - Quick and easy bulk editor for products and variants.
SafeShip - Address Validator - International address validation and PO box blocking at checkout for Shopify Plus merchants.
BohdanNikitchuk
Shopify Partner
5 0 2

Unfortunately, no

DavidT
Shopify Partner
38 2 15

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?

QuickEdit - Bulk Product Edit - Quick and easy bulk editor for products and variants.
SafeShip - Address Validator - International address validation and PO box blocking at checkout for Shopify Plus merchants.
BohdanNikitchuk
Shopify Partner
5 0 2

Hmm, it sounds like a possible solution, must try. Thank you!

jam_chan
Shopify Partner
920 23 188

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

BYOB - Build Your Own Bundles, SPO - SEO App to research keywords & edit social link preview

samuelvandeven
Shopify Partner
2 0 0

Did you found a solution?

lpici
Shopify Partner
6 0 0

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.