-
-
Notifications
You must be signed in to change notification settings - Fork 313
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
ProductVariant CompareAtPrice can't be set to empty #373
Comments
This is related #284. The CompareAtPrice property is not included if the value is null, which means it can't be unset. That's a bug obviously, but the fix isn't any better since by including the null value you can then accidentally remove it when updating a variant too. I'm actively working on finding a better solution for updating objects, since either setting has undesired behavior that can bite the dev if they don't know what to watch for. For now you should be able to work around the behavior by creating your own class for the data you're sending, and then extend the ProductVariantService with a custom method that accepts that class. Something like this: public class ProductVariantCompareAtPrice
{
[JsonProperty("price")]
public decimal Price { get; set; }
[JsonProperty("compare_at_price", DefaultValueHandling = DefaultValueHandling.Include)]
public decimal? CompareAtPrice { get; set; }
}
public class CustomProductVariantService : ProductVariantService
{
public CustomProductVariantService(string domain, string token) : base(domain, token) { }
public async Task<ProductVariant> UpdateCompareAtPriceAsync(long variantId, ProductVariantCompareAtPrice data)
{
var req = PrepareRequest($"variants/{variantId}.json");
var content = new JsonContent(new
{
variant = data
});
return await ExecuteRequestAsync<ProductVariant>(req, HttpMethod.Put, content, "variant");
}
} (Edit: updated with correct DefaultValueHandling attribute.) |
I wrote a horrible kludge that seems to work good enough for now.
|
That should do, glad you found a workaround! |
Yep. It seems to work pretty well. Whenever I know I want to update CompareAtPrice, I simply use ProductVariantServiceEx instead of ProductVariantService. Problem solved. In general, I could use ProductVariantServiceEx all the time - since 0.0 as a CompareAtPrice isn't really valid (or at least anything useful) - but whatever, it works when I know I need it. : ) |
Perhaps I'm missing something, but it doesn't seem possible to set CompareAtPrice to null or empty. :?
The text was updated successfully, but these errors were encountered: