Example of CRUD operations for Metafields with shopify_app gem

Solved
Highlighted
Excursionist
13 3 0

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!

 

 

0 Likes
Highlighted
Excursionist
13 3 0
Is there somewhere else this question should be asked? Basically the expectation was that Shopify would have an API / documentation similar to Stripe. Stripe clearly shows how to Created, Retrieve, Update and Delete objects with their API. For example a Customer: https://stripe.com/docs/api/customers The main question is: Are there methods beyond "ShopifyAPI::Shop.current.add_metafield" - like update, and delete? I have not been able to find documentation / examples. 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 Thank you!
0 Likes
Highlighted
Shopify Partner
4 1 2

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.

0 Likes
Highlighted
Excursionist
13 3 0

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.

Basically this string needs to be added to the URL of the bulk editor page to make the __my_metafield_name metafield show up.
%2Cmetafields.global.__my_metafield_name%3Astring
or
,metafields.global.__my_metafield_name:string
 
Needs to be added at the end of the 'edit=sku%2Cprice%2Ccompare_at_price' parameter
 
Essentially edit= controls what fields show up in the bulk editor.
 
Wish this was a more documented feature but also worry about relying on it since it's not a well documented feature.

 

Thanks again!

0 Likes