Quantcast
Channel: Opinion: Shopify Community - Shopify APIs & SDKs
Viewing all articles
Browse latest Browse all 25503

Creating new orders using ruby gem 'shopify_api' (v 3.2.0) by Amir Dorafshar

$
0
0

I'm trying to create a new order using ruby gem 'shopify_api' version 3.2.0.

I confirm that my credentials and endpoints are valid because I can successfully create new products with this gem. 

require 'shopify_api'

API_KEY   = ' { sanitized } '
PASSWORD  = ' { sanitized } '
SHOP_NAME = ' { sanitized } '

shop_url  = "https://#{API_KEY}:#{PASSWORD}@#{SHOP_NAME}.myshopify.com/admin"
ShopifyAPI::Base.site = shop_url

# Create new product
product = ShopifyAPI::Product.new(
  :title => 'product.title',
  :product_type => 'product.product_type',
  :body_html => 'product.body_html',
  :vendor => 'product.vendor',
  :price => 123.45
)
product.save

# Create new order
order = ShopifyAPI::Order.new(
  :line_item => [
    ShopifyAPI::LineItem.new(
      :quantity => 1,
      :variant_id => product.variants.first.id
    )
  ]
)
order.save
p order

The response complains that I "must have at least one line item" but the the line_item object is identical to the example "simple order with only a product variant id" posted on docs (docs.shopify.com/api/order)

#<ShopifyAPI::Order:0x007fc263b900c8
 @attributes=
  {"line_item"=>
    [#<ShopifyAPI::LineItem:0x007fc263b93818
      @attributes={"quantity"=>1, "variant_id"=>550889801},
      @persisted=false,
      @prefix_options={}>]},
 @errors=
  #<ActiveResource::Errors:0x007fc263b906e0
   @base=#<ShopifyAPI::Order:0x007fc263b900c8 ...>,
   @messages={:line_items=>["must have at least one line item"]}>,
 @persisted=false,
 @prefix_options={},
 @remote_errors=
  #<ActiveResource::ResourceInvalid: Failed.  Response code = 422.  Response message = Unprocessable Entity.>,
 @validation_context=nil>

Any hints are greatly appreciated.

 


Viewing all articles
Browse latest Browse all 25503

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>