Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Regularly update variant-level metafields via app/API

Regularly update variant-level metafields via app/API

gut_und_gunstig
Shopify Partner
6 0 1

Hi there! This is my first time posting so please be gentle!

 

I have a Shopify store that sells a limited number of products, but I am looking to expand. I want to build a Shopify app that automates the process of generating a specific string for each product variant that will be saved as a variant-level metafield.

 

I can figure out the coding myself - I would just appreciate some high-level guidance on what exactly I need to build. This is my first time building an app, so any ideas are appreciated.

 

Summary of project:

 

Each product variant in the store needs to have a 'shipping message' metafield. This metafield is a string that will look something like this:

 

'Product ships in 4-6 business days'

 

The exact syntax and date-range of each variant's metafield will be determined by on the basis of a number of factors (product availability, product tags, certain other metafields). I have written the logic for generating this string - so I do not need any guidance on this front.

 

The aforementioned factors that determine the exact syntax and date-range of the string can change on a day-by-day basis. One of the metafields is updated regularly by a third-party app. A variant's availability can change when the product is sold out. I change product's tags fairly regularly too.

 

For this reason, I want to build an app that updates each variant's 'shipping message' metafield on a regular basis - perhaps every hour.

 

I have set up a basic embedded app using the Shopify CLI 3.0. Now I've done this, I'm a bit lost. Here is what I'm guessing I need to do:

1. Set up some kind of simple interface so that the app's user can select a collection that contains all the store's products. 

2. Once this collection has been selected, the ID of that collection will be used to retrieve a list of every product in that collection.

 

3. From there, each of each products' variant can be looped through.

 

4. Once at the variant-level, various information about that variant can be grabbed (product availability, product tags, certain other metafields). 

 

5. With this information, I would (i) generated each variant's shipping message using the pre-written logic, and (ii) replace its current 'shipping_message' metafield with the newly generated shipping message.

 

[repeat steps 2-5 every hour]

____

 

If anybody could give me any pointers, suggestions ideas about which APIs I should use, how they should be implemented, and any best practices that I should follow, I would be really grateful. 

 

Thanks for taking the time to read this!

Replies 2 (2)

SBD_
Shopify Staff
1831 273 421

Hey @gut_und_gunstig 

 

That process sounds solid to me.

 

If the app is for your own store, you could skip the "select a collection" step / hardcode.

 

You'll want to use the Metafields API to create/update metafields. There's a product variant example on this page: https://shopify.dev/docs/api/admin-rest/2023-04/resources/metafield#put-blogs-blog-id-metafields-met...

 

Good luck!

Scott | Developer Advocate @ Shopify 

gut_und_gunstig
Shopify Partner
6 0 1

Thanks very much for your help!