Skip to content
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

Unable to reset Shopify API compare at price #577

Closed
aswanevelder opened this issue Sep 30, 2020 · 5 comments
Closed

Unable to reset Shopify API compare at price #577

aswanevelder opened this issue Sep 30, 2020 · 5 comments

Comments

@aswanevelder
Copy link

I used to be able to set compare_at_price to null after a product was on sale but now I get an error

Example:

Price was: 1000, Compare at 1200

I want to reset it the Price to 1200, with compare at 0.

(422 Unprocessable Entity) compare_at_price: Compare at price needs to be higher than Price

I can neither set it to 0, so how will I disable the compare_at_price to 0 or null or remove it?

var variant = await productVariantService.UpdateAsync(product.VariantId.Value, new ProductVariant()
{
      Price = updatePrice,
      CompareAtPrice = updateCompareAtPrice
});
@ThatBrianK
Copy link

ThatBrianK commented Oct 5, 2020

I am also experiencing this issue. I've been trying to modify the source code by changing the default value handling to
DefaultValueHandling = DefaultValueHandling.Include

however it still does not include the default value of null.
I don't understand much about serialization but in debugging, I can't even see the raw text being sent.
If I send JSON directly in Postman with a value of null, it does in fact work but I can't figure out how to get the ShopifySharp to do that.

@ThatBrianK
Copy link

someone coded a workaround here:
#373

But I can't get this to compile even.
If anyone can assist that would be amazing!

@rohanp1903
Copy link
Contributor

rohanp1903 commented Oct 5, 2020

This is based on #373

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 ex)
        {
            return null;
        }
    }
}

@ThatBrianK
Copy link

Thanks Rohan!
I had to modify it slightly to work for me since my prices showed up as 0.0 not 0.00 but otherwise perfect. I tried posting in my modified code but it was not formatting correctly

Brian

@aswanevelder
Copy link
Author

Thanks guys! Great help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants