Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hello,
Are there any complete examples of standard create, read, updated, and delete operations for Metafields? In this case we are using Ruby on Rails / shopify_app gem.
There are tons of examples to "Build a Shopify App in 5 Minutes" that outline creating a bare-bones app, installing some gems, creating an app in a partner account, and getting it authenticated which is great.
Is there more documentation / direction to make the best use of the Shopify API - in this case, specifically just creating a Custom app for a single store's unique requirements that include some custom Metafields and unique shipping requirements among other things.
It's simple enough to create a Metafield with a static value - as shown in the documentation here:
https://github.com/Shopify/shopify_api/wiki/API-examples#metafields
unless keys.include? "my_key_name" ShopifyAPI::Shop.current.add_metafield(ShopifyAPI::Metafield.new({ description: "My key description", key: "my_key_name",
namespace: "example_key_namespace",
value: 1234,
value_type: "integer"
})) end
Are there methods beyond "ShopifyAPI::Shop.current.add_metafield" - like update, and delete?
Or is the best practice to make HTTP requests from our app to the endpoints using something like httparty gem?
https://shopify.dev/docs/admin-api/rest/reference/metafield
So in general, is the "best practice" to build whatever models / controllers we want in our app. Then output forms / views that would be edited in the store admin. Then take those values / validate in our app, then just make HTTP / REST requests to actually update / save the Metafield values?
It would be helpful to see a complete simple CRUD example for Metafields with REST (and with GraphQL) utilizing the shopify_app / shopify_api gems.
Or maybe I'm just over-complicating things / overlooking something obvious / not finding some documentation?
Thank you!
Solved! Go to the solution
This is an accepted solution.
The Ruby API documentation is pretty scarce, as you've found. What I've discovered through trial and error (use the shopify_console), and use the Ruby list_methods to get an idea. The Shopify API appears to replace existing objects with matching keys/namespace with any new one you provide. So to update an existing metafield, get the field, change the value and save it back on the object. If you don't care about the previous content then just add a new metafield with the same key and namespace and the previous will be overridden.
This is an accepted solution.
The Ruby API documentation is pretty scarce, as you've found. What I've discovered through trial and error (use the shopify_console), and use the Ruby list_methods to get an idea. The Shopify API appears to replace existing objects with matching keys/namespace with any new one you provide. So to update an existing metafield, get the field, change the value and save it back on the object. If you don't care about the previous content then just add a new metafield with the same key and namespace and the previous will be overridden.
Hey AI_Priest1 thank you for the info.
Makes total sense. Guess they don't need an update or delete etc if it just updates existing record / overwrites it.
Maybe it says this in documentation but I missed it.
Using bulk editor
Also learned you can manipulate the bulk editor URL to include any metafield you wish which is essentially a CRUD interface.
It's a shame you can't just bulk import metafields to a standard Shopify store. From my understanding it requires shopify Plus or a 3rd party app like https://excelify.io/documentation/metafields/
Or your own custom app.
But if you edit the URL of the Bulk Editor and add in to the edit parameter of the URL, you can define any metafield.
Thanks again!