问题描述
我曾经能够在产品发售后将compare_at_price设置为null,但是现在我得到了一个错误。这是利用NozzleGear的ShopifySharp C#库。
示例:
价格为:1000,相比之下为1200
我想将其价格重置为1200,比较为0。
(422无法处理的实体)compare_at_price:价格比较需要高于价格
我都不能将其设置为0,那么如何将compare_at_price禁用为0或为null或将其删除?
var variant = await productvariantService.UpdateAsync(product.VariantId.Value,new Productvariant()
{
Price = updatePrice,CompareAtPrice = updateCompareAtPrice
});
解决方法
似乎有一个错误,这是解决方法...
https://github.com/nozzlegear/ShopifySharp/issues/373
创建并使用以下服务
using System;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ShopifySharp;
using ShopifySharp.Infrastructure;
public class ProductVariantServiceEx : ProductVariantService
{
public ProductVariantServiceEx(string myShopifyUrl,string shopAccessToken) : base(myShopifyUrl,shopAccessToken) { }
public virtual async Task<ProductVariant> UpdateAsync(long productVariantId,ProductVariant variant)
{
try
{
// BUGFIX: If variant.CompareAtPrice is set to 0,then force it to empty
string json = new JsonContent(new { variant }).ReadAsStringAsync().Result;
if (json.Contains("\"compare_at_price\":0.00}")
|| json.Contains("\"compare_at_price\":0.00,"))
{
json = json.Replace("\"compare_at_price\":0.00","\"compare_at_price\":\"\"");
var req = PrepareRequest($"variants/{productVariantId}.json");
using var content = new StringContent(json,Encoding.UTF8,"application/json");
await Task.Run(() => Thread.Sleep(500));
return (await ExecuteRequestAsync<ProductVariant>(req,HttpMethod.Put,default,content,"variant")
.ConfigureAwait(false)).Result;
}
return await base.UpdateAsync(productVariantId,variant);
}
catch (Exception)
{
return null;
}
}
}