gorlaz
gorlaz

Reputation: 478

How to attach SKU to Product via BigCommerce API; The field 'product_option_id' is invalid

I've been banging my head against this for a while so am hoping someone can help. Ruby/HTTParty/BigCommerce Gem (where it works).

Error is: The field 'product_option_id' is invalid ; when trying to add an SKU via

    myresponse = HTTParty.post("#{@store_url}products/800/skus",
        :body=>{
          :sku => "111204",
          :options => [
            {
              :product_option_id => 16,
              :option_value_id => 46
            }
          ]
          }.to_json, 
          :headers => {'Content-Type' => 'application/json' }, 
        :basic_auth => @auth)

Basically I can't see/figure out where 'product_option_id' is referring to; you can't directly link options to products via the API that I can see.

There are two other questions on SO re this issue but neither had useful answers for me.

Prior to this, I've;

{"id"=>"800", "keyword_filter"=>"NULL", "name"=>"Opaque Thigh High", "type"=>"physical", "sku"=>"111204P", "description"=>"Opaque nylon thigh high.", "search_keywords"=>nil, "availability_description"=>nil, "price"=>"3.8200", "cost_price"=>"2.1800", "retail_price"=>"0.0000", "sale_price"=>"0.0000", "calculated_price"=>"3.8200", "sort_order"=>"0", "is_visible"=>"true", "is_featured"=>"false", "related_products"=>"NULL", "inventory_level"=>"0", "inventory_warning_level"=>"0", "warranty"=>"NULL", "weight"=>"0.1000", "width"=>"4.0000", "height"=>"0.2500", "depth"=>"8.0000", "fixed_cost_shipping_price"=>"0.0000", "is_free_shipping"=>"false", "inventory_tracking"=>"simple", "rating_total"=>"0", "rating_count"=>"0", "total_sold"=>"0", "date_created"=>"Sat, 27 Sep 2014 11:45:14 +0000", "brand_id"=>"135", "view_count"=>"0", "page_title"=>nil, "meta_keywords"=>"NULL", "meta_description"=>"NULL", "layout_file"=>"product.html", "is_price_hidden"=>"false", "price_hidden_label"=>nil, "categories"=>{"value"=>"2"}, "date_modified"=>"Sat, 27 Sep 2014 11:45:14 +0000", "event_date_field_name"=>nil, "event_date_type"=>"none", "event_date_start"=>nil, "event_date_end"=>nil, "myob_asset_account"=>nil, "myob_income_account"=>nil, "myob_expense_account"=>nil, "peachtree_gl_account"=>nil, "condition"=>"New", "is_condition_shown"=>"true", "preorder_release_date"=>nil, "is_preorder_only"=>"false", "preorder_message"=>nil, "order_quantity_minimum"=>"0", "order_quantity_maximum"=>"0", "open_graph_type"=>"product", "open_graph_title"=>nil, "open_graph_description"=>nil, "is_open_graph_thumbnail"=>"true", "upc"=>"714718006338", "date_last_imported"=>nil, "option_set_id"=>"NULL", "tax_class_id"=>"0", "option_set_display"=>"right", "bin_picking_number"=>nil, "custom_url"=>"/Opaque-Thigh-High/", "primary_image"=>{"id"=>"0", "zoom_url"=>"NULL", "thumbnail_url"=>"NULL", "standard_url"=>"NULL", "tiny_url"=>"NULL"}, "availability"=>"available", "brand"=>{"link"=>{"content"=>"/brands/135", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/brands/135.xml"}}, "downloads"=>{"link"=>{"content"=>"/products/800/downloads", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/downloads.xml"}}, "images"=>{"link"=>{"content"=>"/products/800/images", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/images.xml"}}, "discount_rules"=>{"link"=>{"content"=>"/products/800/discountrules", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/discountrules.xml"}}, "configurable_fields"=>{"link"=>{"content"=>"/products/800/configurablefields", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/configurablefields.xml"}}, "custom_fields"=>{"link"=>{"content"=>"/products/800/customfields", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/customfields.xml"}}, "videos"=>{"link"=>{"content"=>"/products/800/videos", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/videos.xml"}}, "skus"=>{"link"=>{"content"=>"/products/800/skus", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/skus.xml"}}, "rules"=>{"link"=>{"content"=>"/products/800/rules", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/rules.xml"}}, "option_set"=>"NULL", "options"=>{"link"=>{"content"=>"/products/800/options", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/products/800/options.xml"}}, "tax_class"=>{"link"=>{"content"=>"/taxclasses/0", "rel"=>"resource", "href"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/taxclasses/0.xml"}}}},

Option values:

HTTParty.get("#{@store_url}options/25/values", :basic_auth => @auth)

=> #{"value"=>[{"id"=>"42", "option_id"=>"25", "label"=>"Gold", "sort_order"=>"0", "value"=>"Gold"}, {"id"=>"43", "option_id"=>"25", "label"=>"Silver", "sort_order"=>"0", "value"=>"Silver"}, {"id"=>"44", "option_id"=>"25", "label"=>"Red", "sort_order"=>"0", "value"=>"Red"}, {"id"=>"45", "option_id"=>"25", "label"=>"Black", "sort_order"=>"0", "value"=>"Black"}, {"id"=>"46", "option_id"=>"25", "label"=>"Green", "sort_order"=>"0", "value"=>"Green"}, {"id"=>"47", "option_id"=>"25", "label"=>"White", "sort_order"=>"0", "value"=>"White"}]}}

which returns

{"id"=>16, "option_id"=>25, "option_set_id"=>26, "display_name"=>"Colors", "sort_order"=>1, "is_required"=>true, "option"=>{"url"=>"https://store-w1w68tq.mybigcommerce.com/api/v2/options/25.json", "resource"=>"/options/25"}, "values"=>[{"label"=>"Gold", "sort_order"=>0, "value"=>"Gold", "option_value_id"=>42}, {"label"=>"Silver", "sort_order"=>0, "value"=>"Silver", "option_value_id"=>43}, {"label"=>"Red", "sort_order"=>0, "value"=>"Red", "option_value_id"=>44}, {"label"=>"Black", "sort_order"=>0, "value"=>"Black", "option_value_id"=>45}, {"label"=>"Green", "sort_order"=>0, "value"=>"Green", "option_value_id"=>46}, {"label"=>"White", "sort_order"=>0, "value"=>"White", "option_value_id"=>47}]}

So then I go to do the SKU code at the beginning of the question and I get the error mentioned.

I've tried 16, 26, and 25 as the product option ID's without any luck.

The SKU resource is the only one I can see that allows you to tie all this back to a product.

Any help greatly appreciated.

MORE INFO

HTTParty.get("#{@store_url}products/800/options", :basic_auth => @auth)

=> #<HTTParty::Response:0x10 parsed_response=nil, @response=#<Net::HTTPNoContent 204 No Content readbody=true>

CLOSING OUT THE QUESTION

After beret's advice it all works properly. Note that based on my understanding, the below is contrary to the docs.

Put call; 26 being my option_set I'm attaching to the product

      myresponse = HTTParty.put("#{@store_url}products/800",
        :body=>{
          :option_set_id => 26
          }.to_json, 
          :headers => {'Content-Type' => 'application/json' }, 
        :basic_auth => @auth)

Check the options on the product:

HTTParty.get("#{@store_url}products/800/options", :basic_auth => @auth)

Response:

{"options"=>{"option"=>{"id"=>"7", "option_id"=>"25", "display_name"=>"Colors", "sort_order"=>"1", "is_required"=>"true"}}}

Go back to SKU and add it, note the 7 from the product/option;

      myresponse = HTTParty.post("#{@store_url}products/800/skus",
        :body=>{
          :sku => "111204",
          :options => [
            {
              :product_option_id => 7,
              :option_value_id => 46
            }
          ]
          }.to_json, 
          :headers => {'Content-Type' => 'application/json' }, 
        :basic_auth => @auth)

Response:

{"sku"=>{"id"=>"7", "product_id"=>"800", "sku"=>"111204", "cost_price"=>"0.0000", "upc"=>nil, "inventory_level"=>"0", "inventory_warning_level"=>"0", "bin_picking_number"=>nil, "options"=>{"option"=>{"product_option_id"=>"7", "option_value_id"=>"46"}}}}

Upvotes: 2

Views: 932

Answers (1)

petrkotek
petrkotek

Reputation: 4761

product_option_id is referring to Product Option ID, see Product Option Resource (API Reference).

You should be able to get it by requesting /api/v2/products/800/options.xml

If the endpoint above returns empty content, it means, that the product has not been linked with any Option Set. You can do so, by updating the product via PUT request to /api/v2/products/800 with payload like this: { "option_set_id": 14 }

If you are not sure what's the option_set_id, checkout: /api/v2/options

All these features around Options/Option Sets are very generic, which may be confusing at first, but on the other hand handy in some advanced scenarios.

Upvotes: 1

Related Questions