Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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.) #702

Closed
hoangndt opened this issue Mar 18, 2020 · 19 comments
Labels
pre-v10 Issue opened prior to release of v10 of library Stale

Comments

@hoangndt
Copy link

hoangndt commented Mar 18, 2020

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"

@hoangndt
Copy link
Author

hoangndt commented Mar 18, 2020

I tried to update to latest shopify_app and shopify_api version, and change shopify api_version too but nothing works

@iamkristian
Copy link

I get the same - it is regardless if I changed those values

@EmmaB
Copy link

EmmaB commented Mar 18, 2020

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
Copy link

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

@EmmaB
Copy link

EmmaB commented Mar 18, 2020

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

@iamkristian
Copy link

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

@alex-espinoza
Copy link

alex-espinoza commented Mar 18, 2020

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?

EDIT: Saibotx answer below solved this issue for me. I had to do the following:

   new_shopify_product.variants[0].inventory_management = "shopify"
   new_shopify_product.variants[0].attributes = new_shopify_product.variants[0].attributes.except("inventory_quantity", "old_inventory_quantity")
   new_shopify_product.save

@tanema
Copy link
Contributor

tanema commented Mar 18, 2020

This will be fixed in this PR #655

@iamkristian
Copy link

iamkristian commented Mar 18, 2020

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

@Saibotx
Copy link

Saibotx commented Mar 20, 2020

@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
Copy link
Contributor

tanema commented Mar 20, 2020

@Saibotx what api version are you on?

@Saibotx
Copy link

Saibotx commented Mar 20, 2020

@Saibotx what api version are you on?

@tanema 2020-01

@tanema
Copy link
Contributor

tanema commented Mar 23, 2020

It seems like there was an issue in the PR deploy in #655, the team is working on a fix right now for it.

@cfraga
Copy link

cfraga commented Apr 23, 2020

@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!

@terryjray
Copy link

@tanema I only ran into this after migrating to 2019-10. Will try the variant.attributes.except approach noted above.

@abeering
Copy link

@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.

@dsounded
Copy link

dsounded commented Nov 25, 2021

This is really annoying especially when you work with variant through product object and don't have control over fields

product = ShopifyAPI::Product.find(id)
last_variant = product.variants.last
...

And actually it gives record without those fields but if you do some sort of:

last_variant.price = 10
last_variant.save

it return 2 deprecated fields, workaround is:

last_variant.attributes.except!(:inventory_quantity, :old_inventory_quantity)

@mkevinosullivan mkevinosullivan added the pre-v10 Issue opened prior to release of v10 of library label Mar 9, 2022
@github-actions
Copy link

github-actions bot commented Oct 8, 2022

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

@github-actions github-actions bot added the Stale label Oct 8, 2022
@github-actions
Copy link

We are closing this issue because it has been inactive for a few months.
This probably means that it is not reproducible or it has been fixed in a newer version.
If it’s an enhancement and hasn’t been taken on since it was submitted, then it seems other issues have taken priority.

If you still encounter this issue with the latest stable version, please reopen using the issue template. You can also contribute directly by submitting a pull request– see the CONTRIBUTING.md file for guidelines

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pre-v10 Issue opened prior to release of v10 of library Stale
Projects
None yet
Development

No branches or pull requests