shopify-api-ruby: Edit product raise error: Bad Request (Write requests to inventory_quantity and inventory_quantity_adjustment are no longer supported. Please use the Inventory Levels API.)

I can’t edit any exist products. Everytime I save one, it raise error: ActiveResource::BadRequest (Failed. Response code = 400. Response message = Bad Request (Write requests to inventory_quantity and inventory_quantity_adjustment are no longer supported. Please use the Inventory Levels API.).):

Note that I tested by load random product, then edit it’s title only. this issue only appear today, my application still works fine a couple of days ago.

products = ShopifyAPI::Product.find(:all, :params => {:handle => handle})
product = products[0]
product.title = 'New title'
product.save

Here my gemfile config:

gem 'shopify_app', '12.0.5'
gem 'shopify_api', '9.0.1'

API version:

config.api_version = “2020-01”

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 19 (3 by maintainers)

Most upvoted comments

Yes, we had this yesterday: I don’t know what Shopify API change caused it, but it just kicked in yesterday.

The ShopifyAPI::Product has attributes such as inventory_quantity that are no longer writable, so what you have to do when you retrieve the existing record is to get only the attributes that you want to update:

remote_product = ShopifyAPI::Product.where(
          handle: id,
          fields: %w[id title body_html vendor product_type created_at handle updated_at published_at template_suffix published_scope tags admin_graphql_api_id options images image]
        ).first

Same with the ShopifyAPI::Variant.

remote_variant = ShopifyAPI::Variant.where(
            product_id: remote_product.id,
            fields:     %i[id barcode title price requires_shipping taxable]
          ).first

Hope that helps.

@iamkristian ive found even with the new update (v9.0.3) this is still happening. I’m now doing the following

variant.attributes = variant.attributes.except("inventory_quantity", "old_inventory_quantity")
variant.price = new_price
variant.save

adding that extra line variant.attributes = variant.attributes.except("inventory_quantity", "old_inventory_quantity") basically allows the rest of your code to function as it did before.

@tanema for what it’s worth we just upgraded to 2021-01 and this problem was still persisting. I need to explicity remove "inventory_quantity", "old_inventory_quantity" from attributes before I am able to save a variant.

Worse now though, that the 400 Bad Request doesn’t actually contain any useful information about what is wrong.

Is there any way to get more detailed response information from shopify to debug issues like these?

Thanks.

@tanema is there any update on this issue?

We’ve paused an API migration until this issue was fixed. If the fix is not released in the following weeks we’ll have to resort to implementing the workaround ourselves, so I’d love to have an update so we can plan accordingly. Thanks!

I’m currently creating a new product using the ShopifyAPI::Product.new method:

      new_shopify_product = ShopifyAPI::Product.new
      new_shopify_product.title = product.title
      new_shopify_product.body_html = full_description
      new_shopify_product.vendor = product.designer.name
      new_shopify_product.product_type = product.classification_category
      new_shopify_product.published = product.published
      new_shopify_product.tags = tags
      new_shopify_product.save

The above is working just fine. Then, in the same file under this block of code, I have another block of code that edits the newly created product’s options and variants like so:

      new_shopify_product.options[0].name = "Size"
      new_shopify_product.options[0].values[0] = combined_size
      new_shopify_product.variants[0].price = product.price
      new_shopify_product.variants[0].sku = product.generated_id
      new_shopify_product.variants[0].compare_at_price = product.estimated_retail_price
      new_shopify_product.variants[0].option1 = combined_size
      new_shopify_product.save

I get this 400 error thrown when attempting to call new_shopify_product.save the second time. I’ve been trying to figure out how to use that fields call with the code I currently have, but no luck. Any ideas?

@alex-espinoza I learned that when I call save - the api reloads the full object with all fields. Pretty annoying

Just want to add my experience using the where clause. It will give you whatever if you use where(id: ...).

remote_product = ShopifyAPI::Product.where(
          handle: id,
          fields: %w[id title body_html vendor product_type created_at handle updated_at published_at template_suffix published_scope tags admin_graphql_api_id options images image]
        ).first

Eventually, I dug this out:

ShopifyAPI::Product.find(shopify_id, params: { fields: %i[id title] })

This will give a 404 if not found, and the product if found returning the fields I want.

If I use where, it will just take the next available product - highly unexpected behaviour.

Thanks again @EmmaB for pointing me in the right direction

Yes, it took a bit of looking through the docs, in the Endpoints section here: https://shopify.dev/docs/admin-api/rest/reference/products/product

@EmmaB Excellent! Thanks a bunch, that really helps. I’ll try that. Didn’t even know could do that