コード例 #1
0
ファイル: Shopify.cs プロジェクト: raboud/Optimal
        static async public Task <List <Product> > GetProducts(bool withMetafields = false)
        {
            List <Product> products = new List <Product>();

            try
            {
                int count = await ProductService.CountAsync();

                int pages = (count / 250) + 1;
                for (int page = 1; page <= pages; page++)
                {
                    ProductFilter filter = new ProductFilter
                    {
                        Limit = 250,
                        Page  = page
                    };
                    products.AddRange(await ProductService.ListAsync(filter));
                }
                if (withMetafields)
                {
                    foreach (Product prod in products)
                    {
                        prod.Metafields = await MetaFieldService.ListAsync(prod.Id.Value, "products");
                    }
                }
            }
            catch (Exception e)
            {
            }
            return(products);
        }
コード例 #2
0
        public async Task <Product> GetProduct(ProductDetailModel model)
        {
            Product product = new Product();
            string  msg;

            try
            {
                var allProducts = new List <Product>();
                var service     = new ProductService(model.shopifyurl, model.token);
                product = await service.GetAsync(Convert.ToInt64(model.productId));

                var mservice = new MetaFieldService(model.shopifyurl, model.token);
                foreach (var vgt in product.Variants)
                {
                    var vmetafields = await mservice.ListAsync(Convert.ToInt64(vgt.Id), "variants");

                    vgt.Metafields = vmetafields.Items;
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message.ToString();
            }
            return(product);
        }
コード例 #3
0
        public async Task <string> AddMetaFeilds(List <Product> products, string shopifyurl, string token)
        {
            foreach (var item in products)
            {
                long id      = Convert.ToInt64(item.Id);
                var  service = new MetaFieldService(shopifyurl, token);
                var  count   = await service.CountAsync(id, "products");

                var metafields = await service.ListAsync(id, "products");

                foreach (var item1 in metafields.Items)
                {
                    await service.DeleteAsync(Convert.ToInt64(item1.Id));
                }

                var metafield1 = new MetaField()
                {
                    Namespace     = "products/update",
                    Key           = "myKey",
                    Value         = "5",
                    ValueType     = "integer",
                    Description   = "This is a test meta field. It is an integer value.",
                    OwnerResource = "products/update",
                };
                //Create a new metafield on a product
                metafield1 = await service.CreateAsync(metafield1, id, "products");
            }

            return("");
        }
コード例 #4
0
        public async Task <IActionResult> CreateProduct([FromBody] ProductDetailModel model)
        {
            var response = "";

            try
            {
                //model.token = new HomeController().ReturnToken();
                var service = new ProductService(model.shopifyurl, model.token);
                for (int i = 0; i < 1000; i++)
                {
                    var product = new Product()
                    {
                        Title       = "Product with metafield",
                        Vendor      = "Burton",
                        BodyHtml    = "<strong>Good snowboard!</strong>",
                        ProductType = "Snowboard",
                        Images      = new List <ProductImage>
                        {
                            new ProductImage
                            {
                                Attachment = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
                            }
                        },
                    };
                    product = await service.CreateAsync(product);

                    var mservice  = new MetaFieldService(model.shopifyurl, model.token);
                    var metafield = new MetaField()
                    {
                        Namespace   = "myNamespace",
                        Key         = "myKey",
                        Value       = "5",
                        ValueType   = "integer",
                        Description = "This is a test meta field. It is an integer value."
                    };

                    //Create a new metafield on a product
                    //metafield = await mservice.CreateAsync(metafield,Convert.ToInt64(product.Id), "products");
                    //var value = await mservice.GetAsync(Convert.ToInt64(metafield.Id));
                    //product = await service.GetAsync(Convert.ToInt64(product.Id));
                }
                //var task = Task.Run(async () => await mservice.ListAsync(Convert.ToInt64(product.Id), "products"));
                //var result = task.Result;
                //By default, creating a product will publish it. To create an unpublished product:+1:
                //product = await service.CreateAsync(product, new ProductCreateOptions() { Published = false });
                response = "Product is added successfuly!";
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
            return(Ok(response));
        }
コード例 #5
0
        public async Task <List <Product> > GetAllProducts(ProductDetailModel model)
        {
            Product        product  = new Product();
            List <Product> Products = new List <Product>();
            string         msg;

            try
            {
                var allProducts = new List <Product>();
                var service     = new ProductService(model.shopifyurl, model.token);
                var page        = await service.ListAsync(new ProductListFilter
                {
                    Limit = 250,
                });

                //allProducts.AddRange(page.Items);
                while (true)
                {
                    allProducts.AddRange(page.Items);

                    if (!page.HasNextPage)
                    {
                        break;
                    }

                    page = await service.ListAsync(page.GetNextPageFilter());
                }
                Products = allProducts;
                var mservice = new MetaFieldService(model.shopifyurl, model.token);
                foreach (var item in Products)
                {
                    var metafields = await mservice.ListAsync(Convert.ToInt64(item.Id), "products");

                    string value = JsonConvert.SerializeObject(metafields.Items);
                    item.Metafields = metafields.Items;
                    foreach (var vgt in item.Variants)
                    {
                        var vmetafields = await mservice.ListAsync(Convert.ToInt64(vgt.Id), "variants");

                        vgt.Metafields = vmetafields.Items;
                    }
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message.ToString();
            }
            return(Products);
        }
コード例 #6
0
 public static void InitServices(string updateInventoryAPIFile = null, string updateVariantAPIfile = null, string updateMetafieldsAPIFile = null)
 {
     if (updateInventoryAPIFile != null)
     {
         updateInventoryService = ShopifyAPIConfig.MakeService <InventoryLevelService>(updateInventoryAPIFile);
     }
     if (updateVariantAPIfile != null)
     {
         updateVariantService = ShopifyAPIConfig.MakeService <ProductVariantService>(updateVariantAPIfile);
     }
     if (updateMetafieldsAPIFile != null)
     {
         updateMetafieldService = ShopifyAPIConfig.MakeService <MetaFieldService>(updateMetafieldsAPIFile);
     }
 }
コード例 #7
0
ファイル: Shopify.cs プロジェクト: raboud/Optimal
        internal static async Task <Product> GetProductAsync(long value)
        {
            Product product = null;

            try
            {
                product = await ProductService.GetAsync(value);

                product.Metafields = await MetaFieldService.ListAsync(value, "products");

                foreach (ProductVariant pv in product.Variants)
                {
                    pv.Metafields = await MetaFieldService.ListAsync(pv.Id.Value, "variants");
                }
            }
            catch (Exception e)
            {
            }
            return(product);
        }
コード例 #8
0
ファイル: Shopify.cs プロジェクト: raboud/Optimal
        static public bool CreateMetadata(string type, long id, string ns, string key, object value, string description = null)
        {
            if (value == null)
            {
                return(false);
            }
            string valueType;

            if (value is int)
            {
                valueType = "integer";
            }
            else if (value is string)
            {
                valueType = "string";
                if (string.IsNullOrEmpty(value as string))
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
            MetaField meta = new MetaField()
            {
                Namespace = ns,
                Key       = key,
                Value     = value,
                ValueType = valueType
            };
            MetaField t = MetaFieldService.CreateAsync(meta, id, type).Result;
            IEnumerable <MetaField> metas = MetaFieldService.ListAsync(id, type).Result;

            return(true);
        }
コード例 #9
0
 public ShopifyMetaFieldService(MetaFieldService metaFieldService)
 {
     _metaFieldService = metaFieldService;
 }