Selecting a specific metafield for a product using Shopify API, Ruby on Rails
Asked Answered
W

5

5

In liquid templates this is achieved like so:

{{ product.metafields.book.author }}

Which returns the value of 'author' for it's key 'book'

I'm using Shopify API and Ruby on Rails and have successfully looped over each metafield for a given product:

In the controller:

@products = ShopifyAPI::Product.find(:all, :params => {:limit => 10})

In the view:

<% @products.metafields.each do |metafield| %>
<%= metafield.key %> : <%= metafield.value %>
<% end %>

This returns all of the metafields for a product, as expected. How do I return only those metafields matching a specific key i.e. 'book' from the example above?

Wallasey answered 18/7, 2012 at 9:12 Comment(0)
W
1

This works:

ShopifyAPI::Metafield.find(:first,:params=>{:resource => "products", :resource_id => 94549954, :key => "book"}) 
Wallasey answered 23/7, 2012 at 7:46 Comment(0)
C
10
# add metafield
product = ShopifyAPI::Product.find(product_id)
product.add_metafield(ShopifyAPI::Metafield.new({
   :description => 'Author of book',
   :namespace => 'book',
   :key => 'author',
   :value => 'Kurt Vonnegut',
   :value_type => 'string'
}))

# retrieve metafield
author = ShopifyAPI::Metafield.find(:first,:params=>{:resource => "products", :resource_id => product.id, :namespace => "book", :key => "author"}).value

More info: http://www.shopify.com/technology/3032322-new-feature-metafields

Consentient answered 31/12, 2012 at 1:32 Comment(0)
W
2

This seems to do the trick:

<% product.metafields.each do |metafield| %>
    <% if metafield.key == "book" %>
        <%= metafield.key %>: <%= metafield.value %><br/>
    <% end %>
<% end %>

or

<% product.metafields.each do |metafield| %>
    <% if metafield.key.include?("book") %>
        <%= metafield.key %>: <%= metafield.value %><br/>
    <% else %>
<% end %>
Wallasey answered 18/7, 2012 at 10:38 Comment(2)
I imagine this is better off in the controller, rather than the view. Any adice would be coolWallasey
You wouldn't use that code in the controller, as you are printing out the metafields, which is what you naturally do in the viewRimbaud
W
1

This works:

ShopifyAPI::Metafield.find(:first,:params=>{:resource => "products", :resource_id => 94549954, :key => "book"}) 
Wallasey answered 23/7, 2012 at 7:46 Comment(0)
C
0
ShopifyAPI::Metafield.find(:all,:params=>{:product_id => product.id, :key=> 'book'})
Col answered 19/7, 2012 at 2:43 Comment(3)
Hi John, thanks for your answer. I'm having some trouble implementing this, would you mind elaborating on the code a little please?Wallasey
This for me just returns an empty array. I've tried in IRB using minimal params i.e. ShopifyAPI::Metafield.find(:all,:params=>{:product_id => 94549896})Wallasey
This works: ShopifyAPI::Metafield.find(:first,:params=>{:resource => "products", :resource_id => 94549954, :key => "book"})Wallasey
A
0

You can get all metafields for a product

@product = ShopifyAPI::Product.find(params[:id])

After that, you can get its metafields

@metafields = @product.metafields

Or if you need just a specific metafield for that product

@metafield = ShopifyAPI::Product::Metafield.find(:all, where: {"product_id = ? AND metafield.key = ?", product.id, "book"})
Amplify answered 29/12, 2016 at 1:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.