Creating a new product option / property

Marcus
Shopify Partner
8 0 0
Hi Guys, I'd like to create a new product option/property name via activeresource for the purpose of holding my suppliers stock. I would have thought it would be something like this:

  p = Product.find(PRODUCT_ID)
  p.options.option(:name => "Marcus").save
 
but alas it's not the case, any ideas? Cheers, Marcus
0 Likes
Marcus
Shopify Partner
8 0 0

bump
maybe some rails guys like Cody or Toby would know?

0 Likes
Edward_Ocampo-G
Shopify Staff
Shopify Staff
195 0 3

Hi Marcus

What you’ll have to do is add a new Product Option name, add Product Variant values for that option, and only then save the product.

Example code looks like this:


  product = ShopifyAPI::Product.find(:first)
  product.options.size # => let’s say that it returns 2, so option3 is available

  product.options << ShopifyAPI::Product::Option.new(:name => "Happiness")
  product.variants.each_with_index {|v, i| v.option3 = "Happiness level #{i}" }
  product.save

Remember that each Product can only have up to 3 option names; any more and you’ll run into errors.

Edward Ocampo-Gooding – Shopify Developer Advocate
0 Likes
Marcus
Shopify Partner
8 0 0

Thanks Edward,

I’m just a simple Perl/PHP guy trying to make the transition to ruby. I’m guessing that this is using the ShopifyAPI within the rails example App rather than using ActiveResource?

Which I guess also means that I can’t use the above code without writing a rails app (just started ruby, haven’t managed to get onto rails yet).

Is the ShopifyAPI available as a gem?

Alternatively is there a way to do exactly this using ActiveResource? (I’ve managed to get my head sort of round ActiveResource).

Love your work!

Cheers,

Marcus

0 Likes
Marcus
Shopify Partner
8 0 0

Hi Edward,

Thanks for your help. What we ended up doing is pulling the shopify_api file
from the rails example code and using the following code:


#!/usr/bin/env ruby

PRODUCT_ID = ARGV[0]
INVENTORY_QUANTITY = ARGV[1]
SUPPLIER_INVENTORY_QTY = ARGV[2]

require 'rubygems'
require 'activeresource'
require 'activesupport'
require 'shopify_api'

KEY = 'MYKEYHERE'
PASS = 'MYPASSHERE'

BASE_SITE = "https://#{KEY}:#{PASS}@lbestore.myshopify.com/admin" 

ActiveResource::Base.site = BASE_SITE

product = ShopifyAPI::Product.find(PRODUCT_ID)
option = product.options.select { |o| o.name == "Supplier Inventory" }.first

unless option
  option = ShopifyAPI::Product::Option.new(:name => "Supplier Inventory")  
  product.options << option
end

product.variants.each_with_index do |variant, index|
  variant.option2 = SUPPLIER_INVENTORY_QTY
end

product.save

I hope this code helps others in their shopify automation!

Cheers,

Marcus

0 Likes
Edward_Ocampo-G
Shopify Staff
Shopify Staff
195 0 3

Hey Marcus!

Wow… I… never thought of using a product option to store a secondary inventory. Nice hack!

If you’re just getting into Rails, check out the Rails Guides [http://guides.rubyonrails.org/] for the time being, and my own book when it comes out online (it’ll be announced through Twitter at @railsnutshell)

As an aside, I’d appreciate if you took a quick stroll through the Shopify App tutorial I wrote – you’re in the audience I’m aiming at with it and can spot the pitfalls I’ve missed: http://forums.shopify.com/categories/9/posts/29661

Let me know if you run into any other API issues. Happy Hacking!

Edward Ocampo-Gooding – Shopify Developer Advocate
0 Likes
Edward_Ocampo-G
Shopify Staff
Shopify Staff
195 0 3

(bump)

If anyone is thinking of using product options to store secondary inventory today, take a look at Metafields instead - this is the sort of thing they're great at.

Edward Ocampo-Gooding – Shopify Developer Advocate
0 Likes
jriles
New Member
1 0 0

Does this still work?

0 Likes