我正在尝试使用restsharp和newton json包连接到magento 2,并尝试使用rest api更新产品数据。但是,我收到此错误:
“ Property StockItem没有访问器方法getStockItem”
我不确定是什么问题。从我看到的内容来看,我的Json请求看起来正确,代码如下。有什么想法吗?
{ "saveOptions": true, "product": { "sku": "1234", "price": 1250,
"stock_item": { "item_id": 0, "product_id": 0, "stock_id": 0, "qty": 1,
"is_in_stock": true, "is_qty_decimal": false,
"show_default_notification_message": false, "use_config_min_qty": false,
"min_qty": 0, "use_config_min_sale_qty": 0, "min_sale_qty": 0,
"use_config_max_sale_qty": false, "max_sale_qty": 0,
"use_config_backorders": false, "backorders": 0,
"use_config_notify_stock_qty": false, "notify_stock_qty": 0,
"use_config_qty_increments": false, "qty_increments": 0,
"use_config_enable_qty_inc": false, "enable_qty_increments": false,
"use_config_manage_stock": false, "manage_stock": false, "low_stock_date": null,
"is_decimal_divided": false, "stock_status_changed_auto": 0 } } }
public string UpdateProduct(string token, string sku, decimal price, decimal stocklevel)
{
var request = CreateRequest("/rest/all/V1/products/" + sku, Method.PUT, Token);
var prod = new ProductItem();
var item = new M2Product();
item.Price = Convert.ToInt32(price);
item.Sku = sku;
var StockItem = new StockItem();
item.StockItem = StockItem;
StockItem.Qty = Convert.ToInt32(stocklevel);
prod.Product = item;
prod.SaveOptions = true;
if (Convert.ToInt32(stocklevel) == 0)
{
StockItem.IsInStock = false;
}
else StockItem.IsInStock = true;
string json = JsonConvert.SerializeObject(prod, Formatting.Indented);
request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = Client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return response.Content;
}
else
{
return ":(" + response.Content;
}
}
public class StockItem
{
[JsonProperty("item_id")]
public int ItemId { get; set; }
[JsonProperty("product_id")]
public int ProductId { get; set; }
[JsonProperty("stock_id")]
public int StockId { get; set; }
[JsonProperty("qty")]
public int Qty { get; set; }
[JsonProperty("is_in_stock")]
public bool IsInStock { get; set; }
[JsonProperty("is_qty_decimal")]
public bool IsQtyDecimal { get; set; }
[JsonProperty("show_default_notification_message")]
public bool ShowDefaultNotificationMessage { get; set; }
[JsonProperty("use_config_min_qty")]
public bool UseConfigMinQty { get; set; }
[JsonProperty("min_qty")]
public int MinQty { get; set; }
[JsonProperty("use_config_min_sale_qty")]
public int UseConfigMinSaleQty { get; set; }
[JsonProperty("min_sale_qty")]
public int MinSaleQty { get; set; }
[JsonProperty("use_config_max_sale_qty")]
public bool UseConfigMaxSaleQty { get; set; }
[JsonProperty("max_sale_qty")]
public int MaxSaleQty { get; set; }
[JsonProperty("use_config_backorders")]
public bool UseConfigBackorders { get; set; }
[JsonProperty("backorders")]
public int Backorders { get; set; }
[JsonProperty("use_config_notify_stock_qty")]
public bool UseConfigNotifyStockQty { get; set; }
[JsonProperty("notify_stock_qty")]
public int NotifyStockQty { get; set; }
[JsonProperty("use_config_qty_increments")]
public bool UseConfigQtyIncrements { get; set; }
[JsonProperty("qty_increments")]
public int QtyIncrements { get; set; }
[JsonProperty("use_config_enable_qty_inc")]
public bool UseConfigEnableQtyInc { get; set; }
[JsonProperty("enable_qty_increments")]
public bool EnableQtyIncrements { get; set; }
[JsonProperty("use_config_manage_stock")]
public bool UseConfigManageStock { get; set; }
[JsonProperty("manage_stock")]
public bool ManageStock { get; set; }
[JsonProperty("low_stock_date")]
public object LowStockDate { get; set; }
[JsonProperty("is_decimal_divided")]
public bool IsDecimalDivided { get; set; }
[JsonProperty("stock_status_changed_auto")]
public int StockStatusChangedAuto { get; set; }
}
public class CustomAttribute
{
[JsonProperty("attribute_code")]
public string AttributeCode { get; set; }
[JsonProperty("value")]
public object Value { get; set; }
}
public class ProductItem
{
[JsonProperty("product")]
public M2Product Product { get; set; }
[JsonProperty("saveOptions")]
public bool SaveOptions;
}
public class M2Product
{
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("price")]
public int Price { get; set; }
[JsonProperty("stock_item")]
public StockItem StockItem { get; set; }
}
设法解决以下问题。.可能会帮助某人。必须使用扩展属性进行更新,但这未在swagger结构中显示。也只更新您需要的字段。
public string UpdateProduct(string token, string sku, decimal price, decimal stocklevel)
{
var request = CreateRequest("/rest/all/V1/products/" + sku, Method.PUT, Token);
var prod = new ProductItem();
var item = new M2Product();
item.Price = Convert.ToInt32(price);
item.Sku = sku;
var STK= new ExtensionAttributes();
var stkitem = new StockItem();
stkitem.Qty = Convert.ToInt32(stocklevel);
prod.Product = item;
item.ExtensionAttributes = STK;
prod.SaveOptions = true;
if (Convert.ToInt32(stocklevel) == 0)
{
stkitem.IsInStock = false;
}
else stkitem.IsInStock = true;
STK.StockItem = stkitem;
string json = JsonConvert.SerializeObject(prod, Formatting.Indented);
request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = Client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return response.Content;
}
else
{
return ":(" + response.Content;
}
}
}
public class StockItem
{
[JsonProperty("qty")]
public int Qty { get; set; }
[JsonProperty("is_in_stock")]
public bool IsInStock { get; set; }
}
public class ProductItem
{
[JsonProperty("product")]
public M2Product Product { get; set; }
[JsonProperty("saveOptions")]
public bool SaveOptions;
}
public class ExtensionAttributes
{
[JsonProperty("stock_item")]
public StockItem StockItem { get; set; }
}
public class M2Product
{
[JsonProperty("price")]
public int Price { get; set; }
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("extension_attributes")]
public ExtensionAttributes ExtensionAttributes { get; set; }
}