我曾经可以在产品打折后将compare_at_price设置为null,但现在我得到了一个错误。这利用了来自NozzleGear的ShopifySharp C#库。
示例:
价格是: 1000,相比之下是1200
我想将它的价格重置为1200,比较为0。
(422无法处理的实体) compare_at_price:价格比较需要高于价格
我既不能将其设置为0,也不能将其设置为0,那么如何将compare_at_price禁用为0或null或将其删除?
var variant = await productVariantService.UpdateAsync(product.VariantId.Value, new ProductVariant()
{
Price = updatePrice,
CompareAtPrice = updateCompareAtPrice
});发布于 2020-10-06 20:01:02
似乎有一个bug,这是解决办法...
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;
}
}
}https://stackoverflow.com/questions/64135760
复制相似问题