无法访问 Shopify 订单的某些方法 API

Can't access some methods of Shopify Order API

这是我第一次尝试创建 Shopify 应用;我要做的就是查询我的商店以获取订单详细信息。

完成教程:https://www.youtube.com/watch?v=DB48N8rRHFw 和 Github 上的 Shopify_app 后,我有一个可以安装到开发商店并进行测试的应用程序。我关闭了默认的 Products 查询,并尝试将其替换为 Orders 查询。

当前代码如下:

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

和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>

我一生都没有理解的是我如何才能完全成功地获得创建日期、总成本、税收等值,但我 运行 遇到了很多问题当涉及到从散列属性中获取值时。我一直使用 https://help.shopify.com/api/reference/order 作为我的 API 参考,我知道它们存在,但是当我 运行 应用程序时,我从 Heroku 日志中得到以下信息:

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'

显然,我不理解这方面可能非常基本的东西。如果没有送货地址,我仍然会返回 "nil" 之类的东西;太奇怪了,错误是方法不存在

编辑:我应该注意 gem 版本以防万一。 shopify_api (4.2.2)、shopify_app (7.0.7)、json (1.8.3),并使用 ruby 2.3.0p0。如果还有其他 gem 可能是原因,请告诉我,我会调查。

据我所知,你提到它 returns 作为哈希,所以你可能想尝试像哈希一样调用它

<%= order['shipping_address']['city'] %>

方法undefined也让我觉得是这样的。因为它说 shipping_address 没有 getter。

我创建的第一个测试订单完全没有客户,这意味着没有可提取的送货地址。我一定是第一次忽略了这个领域,之后就再也没有注意到。并不是它返回 nil 或任何类似的东西,这个字段从一开始就不存在;那是我的问题。

一旦我删除订单并使用具有感兴趣信息的订单进行测试,order.shipping_address.city 就成功了。