nozzlegear / ShopifySharp

ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
https://nozzlegear.com/shopify-development-handbook
MIT License
742 stars 308 forks source link

Product Variant Metafields #1024

Open waynevanrooyen1971 opened 6 months ago

waynevanrooyen1971 commented 6 months ago

I am trying to get the variant metafields using below code and even though i know a product hass metafields all the products are returning metafields ass null. Please explain what i am doing wrong.

var filter = new ProductListFilter { Limit = pageSize, SinceId = sinceId };

            // Get products from the Shopify store
            var productList = await service.ListAsync(filter);

            // Check if there are more products to retrieve
            if (productList.Items.Count() < pageSize)
            {
                hasNextPage = false;
            }
            else
            {
                // Set the sinceId for the next page
                sinceId = productList.Items.Last().Id;
            }

            System.Diagnostics.Debug.WriteLine($" Page Size:" + pageSize + " Has Page:" + hasNextPage);

            foreach (var product in productList.Items)
            {
                if(product.Title.Contains("Finley Stripe See Through"))
                {
                    Debug.WriteLine(product.Title);
                }
                var variantservice = new ProductVariantService(shopDomain, accessToken);
                var variantfilter = new ProductVariantListFilter
                {
                    Limit = 30

                };
                var variants = await variantservice.ListAsync(product.Id ?? 0, variantfilter);
                foreach (var variant in variants.Items)
                {
                    Debug.WriteLine(product.Title + "--" + variant.Title);         
                    Debug.WriteLine(variant.Metafields);
                }
                }
waynevanrooyen1971 commented 6 months ago

var service = new MetaFieldService(myShopifyUrl, shopAccessToken); var metafields = await service.ListAsync(productId, "products");

Seems like "products" should me a listfilter, even though changed to one still not getting any

waynevanrooyen1971 commented 6 months ago

My BAD wass doin it all wrong

waynevanrooyen1971 commented 6 months ago

OK bit of an issue here with variant metafields if the metafield does not have some sort of existing value there is no way of you getting and id as far as i can see so i want to write size chart info into a metafield like so

var mpnmetafield = await metafieldservice.UpdateAsync(metafield.Id ?? 0, new MetaField() { Value = xxxxxxl });

cant do it as metafield.Id does not exist until there is already an actual value. I had to update all the metafield manually via UI then ran same code and it worked. Found same issue on the community for shopify. Just a heads up if anyone hass the same issue