Using script console in development
From Shopify Wiki
Using script/console to play with your shop's resources via the API can save you a lot of pain when developing a Shopify app. You can always use the curl 'command line' tool to speak to your shop's resources, but you are not writing any Ruby code that way, and, most importantly, you are not leveraging the power of the shopify_api gem.
You're using a private application rather than an application created in your partner account? We've got you covered with using script/console with the credentials of a private application.
Contents |
Setting up your environment
Do steps 1 to 9 of the guide Creating an app and deploying it to Heroku.
Understanding script/console
You run script/console this way from the root of your application folder.
ruby script/console
With script/console, you can speak to your app from the command line. It is very similar to writing JavaScript in the Firebug's console.
As this old article by Amy Hoy puts it:
The console lets you fiddle with parts of your Rails app—models especially—in a real-time environment, instead of waiting for page reloads and clicking on links and using object dumps or breakpoints to try and get a picture of what’s going on behind the scenes.
One small caveat
The console.rb script gives you access to the full context of your application with one exception: it does not give you access to the session. The session is where your application stores the username and password required to access your shop's resources. But we can work around this restriction as you will see in the next step.
The handy reload! command
Whenever you make changes to your app's code, remember to run this command in your console:
>> reload!
This will reload your application environment, so you don't need to 'quit' and run script/console again.
Telling ShopifyAPI::Base how to access your shop's resources
You need to tell the ShopifyAPI::Base class about the URL of your shop, and the username and password to be used for it.
The site property of ActiveResource::Base is where we store this information. ShopifyAPI::Base inherits from ActiveResource::Base, so we can type in this code in the script console:
>> ShopifyAPI::Base.site = "https://username:password@some-shop.myshopify.com/admin"
What's our username and password, though?
By implanting a little bug in our code, we'll find out.
Open the file app/controllers/home_controller.rb in your text editor, and paste the following code somewhere inside the index action:
puts 'The URL should be: ' + ShopifyAPI::Base.site.to_s
Save.
Now visit your home page again. Do a refresh in the browser if you're there already.
Now go the the console in which you started your application's server. The puts code wrote something to it. Copy and paste the URL you see there in the console in which you are running script/console. Don't forget to wrap your URL in quotes: ShopifyAPI::Base.site expects a string.
Now play
You now have 'command line' read and write access to your shop's resources.
Let's have fun with product options
You are not sure how to add options to a product using the Shopify API? That's okay, let's fudge around until we get it.
Here's a screen capture of the Product Details page of my 'Crappy T-shirt' product. I would like to add sizes and colors to this product.
Let's start by storing a reference to the product in a p variable.
>> p = ShopifyAPI::Product.find(11128132)
I won't show you what the above code returns because that covers about a 100 miles, but we have our product.
It is an instance of ShopifyAPI::Product.
Let's look at the options associated with that product:
>> p.options
The above returns an array of one ShopifyAPI::Product::Option objects.
=> [#<ShopifyAPI::Product::Option:0x101c125d0 @prefix_options={}, @attributes={"name"=>"Title"}>]
The brackets that wrap this tell us we're dealing with an array.
Let's see if we can change the option name from 'Title' to 'Size'.
>> p.options
=> [#<ShopifyAPI::Product::Option:0x1020ca2a8 @prefix_options={}, @attributes={"name"=>"Title"}>]
>> p.options.first
=> #<ShopifyAPI::Product::Option:0x1020ca2a8 @prefix_options={}, @attributes={"name"=>"Title"}>
>> p.options.first.name = 'Size'
=> "Size"
>> p.save
=> true
Our save returns true.
Let's take a look at the Product Details page now.
It worked. The option name is now 'Size'.
Let's change that variant title from 'Default' to 'Small'. Then we will add a 'Medium' size. And move on to adding Colors.
>> p.variants.first.option1 => "Default" >> p.variants.first.option1 = 'Small' => "Small" >> p.save => true
Did that work? Apparently so:
p.variants returns an array of ShopifyAPI::Variant objects. See if we can add a variant to our product.
>> p.variants.size => 1
Let's add a 'Medium' variant. We will only specify option1 and the "price". We will let Shopify fill in all the blanks with default values.
>> p.variants << ShopifyAPI::Variant.new({ :option1 => 'Medium', :price => 12 })
=> [#<ShopifyAPI::Variant:0x102250960 @prefix_options={:product_id=>11128132}, @attributes={"price"=>#<BigDecimal:1022c9a68,'0.19E2',4(8)>, "position"=>1, "created_at"=>Thu Nov 26 21:37:20 UTC 2009, "title"=>"Small", "requires_shipping"=>true, "compare_at_price"=>nil, "updated_at"=>Mon Jan 11 22:08:55 UTC 2010, "inventory_quantity"=>0, "inventory_policy"=>"continue", "inventory_management"=>nil, "id"=>28456722, "taxable"=>true, "grams"=>0, "sku"=>"", "option1"=>"Small", "option2"=>nil, "fulfillment_service"=>"manual", "option3"=>nil}>, #<ShopifyAPI::Variant:0x1021da328 @prefix_options={}, @attributes={"option1"=>"Medium","price"=>#<BigDecimal:1022c9a68,'0.19E2',4(8)>}>]
>> p.save
=> true
Did this work? Yes:
Let's add Colors. Just one: 'Red'.
>> p.options << ShopifyAPI::Product::Option.new({ :name => 'Color', :value => 'Red' })
=> [#<ShopifyAPI::Product::Option:0x101ec3108 @prefix_options={}, @attributes={"name"=>"Size"}>, #<ShopifyAPI::Product::Option:0x101e3e548 @prefix_options={}, @attributes={"name"=>"Color", "value"=>"Red"}>]
>> p.save
=> true
Note
When adding an option to a product, the value attribute must be set to the 'default value' for that option.
Let's see what we've got now:
More examples, please
For other examples of what you can do with script/console using the shopify_api gem, head over this other tutorial: Using the shopify api gem with the credentials of a private application.





