I'm using shopify_api (https://github.com/Shopify/shopify_api) to create an app for Shopify using Ruby.
For a Base::Product
, by calling directly product.variants
, i will have:
[#<ShopifyAPI: :Variant: 0x00007fa15cb0e960 @attributes={
"id"=>12664776392816,
"title"=>"Default Title",
"price"=>"5.00",
"sku"=>"",
"position"=>1,
"inventory_policy"=>"deny",
"compare_at_price"=>nil,
"fulfillment_service"=>"manual",
"inventory_management"=>nil,
"option1"=>"Default Title",
"option2"=>nil,
"option3"=>nil,
"created_at"=>"2018-08-27T03:17:24-04:00",
"updated_at"=>"2019-04-07T23:52:00-04:00",
"taxable"=>true,
"barcode"=>"",
"grams"=>0,
"image_id"=>nil,
"weight"=>0.0,
"weight_unit"=>"kg",
"inventory_item_id"=>12758757474416,
"inventory_quantity"=>0,
"old_inventory_quantity"=>0,
"requires_shipping"=>true,
"admin_graphql_api_id"=>"gid://shopify/ProductVariant/12664776392816"
}, @prefix_options={
:product_id=>1389200408688
}, @persisted=true>
]
In this case, how do I directly get price
attribute from this json returned
EDIT: I just jump in ruby on rails in the middle, so here is what I have tried so far:
product.variants.prices
--> in my guts it definitely does not work, but might as well trying
returns with undefined method
price' for #`
parse the JSON
1) JSON.parse(product.varient)['price']
returns with
no implicit conversion of Array into String
2) variant = ActiveSupport::JSON.decode(product.variants[0])
or variant = ActiveSupport::JSON.decode(product.variants)
then
variant['price']
but both return with no implicit conversion of ShopifyAPI::Variant into String
product = ShopifyAPI::Product.find(shopify_product_id)
product.variants.map(&:price)
it will give you an array of price because product might have multiple variants. you can also use .pluck method instead of .map