2

What is the correct way to add a product variant?

I create the product successfully and it shows up in my Shopify admin. However the price is always zero and the quantity is always infinity.

I've tried creating a variant with inventory_quantity and price set, as well as with product_id set in prefix_options.

However admin is always showing price zero and quantity infinity.

Rails v3.2.5
shopify_api v3.0.0
shop name: vacation-2

I don't get errors doing the API calls. I simply don't see my variant data in the product in admin.

Edward Ocampo-Gooding
  • 2,782
  • 1
  • 23
  • 29
realdeal
  • 153
  • 1
  • 11

2 Answers2

6

Make sure that you set the :inventory_management attribute to "shopify", or the quantity won't persist.

I just tested this out, and it works perfectly:

product.variants << ShopifyAPI::Variant.new(
  :option1              => "Large",
  :price                => 12.95,
  :inventory_management => 'shopify',
  :inventory_quantity   => 10
)
product.save
Travis
  • 735
  • 7
  • 12
  • 1
    Thanks, this worked. Couldn't find in documentation on Shopify but I guess that's why they have a wiki! Here is what I ended up actually using:
    shopify_listing = ShopifyAPI::Product.find(listing.shopify_id
    shopify_listing.update_attributes(listing_data)
    variant = ShopifyAPI::Variant.find(shopify_listing.variants.first.id)
    variant.update_attributes({inventory_quantity: listing.quantity, inventory_management: 'shopify', price: listing.price})
    – realdeal Jun 12 '12 at 16:54
0

Adding variants without using the products association:

ShopifyAPI::Variant.new(
  :product_id           =>  #enter product id,
  :option1              => "Large",
  :price                => 12.95,
  :inventory_management => 'shopify',
  :inventory_quantity   => 10
)
product.save

The advantage here is that variant object is updated with the values returned by the API.

lcharbon
  • 3,030
  • 2
  • 16
  • 18