Ad

Can't Access Some Methods Of Shopify Order API

- 1 answer

This is my first time attempting to create a Shopify App; all I'm trying to do is query my store for order details.

After having completed the tutorials: https://www.youtube.com/watch?v=DB48N8rRHFw and the Shopify_app on Github, I have an app that I can install to a development store and test. I switched out the default Products query and have attempted to replace it with a query on Orders instead.

The current code is as follows:

class HomeController < ShopifyApp::AuthenticatedController
  def index
    @orders = ShopifyAPI::Order.find(:all)
  end
end

and index.html.erb:

<% content_for :javascript do %>
  <script type="text/javascript">
    ShopifyApp.ready(function(){
      ShopifyApp.Bar.initialize({
        title: "Home",
        icon: "<%= asset_path('favicon.ico') %>"
      });
    });
  </script>
<% end %>

<h2>Orders</h2>


<table border="1" style="width:100%">
  <tr>
    <th>Order ID</th>
    <th>Recipient</th>
    <th>Shipping Address</th>
    <th>Total Order</th>
    <th>Date of Order</th>
    <th>Sale Total</th>
    <th>Sales Tax</th>
    <th>Shipping Charge</th>
    <th>Payment Gateway</th>
    <th>Fulfillment Status</th>
  </tr>

  <% @orders.each do |order| %>
  <tr>
    <td><%= order.order_number %></td>
    <td>cust</td>
    <td><%= order.shipping_address.city %></td>
    <td>total</td>
    <td><%= order.created_at %></td>
    <td><%= order.subtotal_price %></td>
    <td><%= order.total_tax %></td>
    <td><%= order.shipping_lines %></td>
    <td><%= order.payment_gateway_names %></td>
    <td><%= order.fulfillment_status %></td>
  </tr>
 <% end %>
</table>

The thing that I do not for the life of me understand is how I can be completely successful getting the values such as the creation date, total cost, tax, etc. but I run into massive issues when it comes to getting the values from the properties that are hashes. Having been using https://help.shopify.com/api/reference/order as my API reference, I know that they exist, however when I run the app, I get the following from the Heroku logs:

2016-06-27T22:47:02.298067+00:00 app[web.1]: Processing by HomeController#index as HTML
2016-06-27T22:47:02.512071+00:00 app[web.1]:   Rendered home/index.html.erb within layouts/embedded_app (2.1ms)
2016-06-27T22:47:02.513471+00:00 app[web.1]:     32:     <td>cust</td>
2016-06-27T22:47:02.513473+00:00 app[web.1]:     36:     <td><%= order.subtotal_price %></td>
2016-06-27T22:47:02.513470+00:00 app[web.1]:     31:     <td><%= order.order_number %></td>
2016-06-27T22:47:02.513468+00:00 app[web.1]: ActionView::Template::Error (undefined method `shipping_address' for #<ShopifyAPI::Order:0x007f27612d5660>):
2016-06-27T22:47:02.513464+00:00 app[web.1]: 
2016-06-27T22:47:02.513469+00:00 app[web.1]:     30:   <tr>
2016-06-27T22:47:02.513471+00:00 app[web.1]:     33:     <td><%= order.shipping_address %></td>
2016-06-27T22:47:02.513472+00:00 app[web.1]:     34:     <td>total</td>
2016-06-27T22:47:02.513473+00:00 app[web.1]:     35:     <td><%= order.created_at %></td>
2016-06-27T22:47:02.513474+00:00 app[web.1]:   app/views/home/index.html.erb:33:in `block in _app_views_home_index_html_erb__4133370917654343554_69903554858260'
2016-06-27T22:47:02.513475+00:00 app[web.1]:   app/views/home/index.html.erb:29:in `_app_views_home_index_html_erb__4133370917654343554_69903554858260'

Obviously, I'm not understanding something probably very fundamental about this. If there didn't exist a shipping address, I'd still get back "nil" or something; it's so weird that the error is that the method doesn't exist.

Edit: I should probably note gem versions in case that is the cause. shopify_api (4.2.2), shopify_app (7.0.7), json (1.8.3), and using ruby 2.3.0p0. If there's another gem that might be the cause, just tell me and I'll look into it.

Ad

Answer

My first test order that I had created lacked a customer outright, meaning no shipping address to pull. I must have simply overlooked the field the first time, and never noticed after. It wasn't that it was returning nil or any such thing, the field just never existed to begin with; that was my problem.

Once I removed the order and tested with ones that had the information of interest, order.shipping_address.city was successful.

Ad
source: stackoverflow.com
Ad