public Product ToProduct(ShopifyProduct shopifyProduct) { var product = new Product { Id = shopifyProduct.Id, Title = shopifyProduct.Title, ImgUrl = shopifyProduct.Images.FirstOrDefault(x => x.Variants.Count == 0)?.Src }; if (string.IsNullOrEmpty(product.ImgUrl)) { product.ImgUrl = shopifyProduct.Images.FirstOrDefault()?.Src; } foreach (var shopifyVariant in shopifyProduct.Variants) { var variant = new Variant { Id = shopifyVariant.Id, Title = shopifyVariant.Title, Price = shopifyVariant.Price, ImgUrl = shopifyProduct.Images.Where(x => x.Variants.Any(y => y == shopifyVariant.Id)) .Select(x => x.Src).ToList() }; product.Variants.Add(variant); } return(product); }
private async Task <ShopifySharp.Product> UploadProduct(ShopifyProduct product) { try { var service = new ShopifySharp.ProductService("https://*****:*****@amore-by-leva.myshopify.com", "1ea0b4f49ff131e180a2e7b7e5cba0d6"); var productToUpload = new ShopifySharp.Product() { Title = product.Title, ProductType = product.ProductType, Vendor = "ASP.Net", BodyHtml = "<b>Such a nice product for this price</b>", }; if (product.Publish) { productToUpload.PublishedAt = DateTimeOffset.Now; } return(await service.CreateAsync(productToUpload, new ShopifySharp.ProductCreateOptions() { Published = product.Publish })); } catch (Exception e) { throw e; } }
public long UpsertProductAndInventory(Product product) { long productMonsterId; using (var transaction = _inventoryRepository.BeginTransaction()) { var existing = _inventoryRepository.RetrieveProduct(product.id); if (existing == null) { var data = new ShopifyProduct(); data.ShopifyProductId = product.id; data.ShopifyTitle = product.title ?? ""; data.ShopifyProductType = product.product_type; data.ShopifyVendor = product.vendor; data.DateCreated = DateTime.UtcNow; data.LastUpdated = DateTime.UtcNow; _executionLogService.Log(LogBuilder.DetectedNewProduct(data)); _inventoryRepository.InsertProduct(data); _inventoryRepository.SaveChanges(); productMonsterId = data.MonsterId; } else { existing.ShopifyTitle = product.title ?? ""; existing.ShopifyProductType = product.product_type; existing.ShopifyVendor = product.vendor; existing.LastUpdated = DateTime.UtcNow; _inventoryRepository.SaveChanges(); productMonsterId = existing.MonsterId; } _shopifyJsonService.Upsert(ShopifyJsonType.Product, product.id, product.SerializeToJson()); transaction.Commit(); } // Write the Product Variants UpsertProduct(productMonsterId, product); // Flags the missing Variants ProcessMissingVariants(productMonsterId, product); // Pull and write the Inventory PullAndUpsertInventory(productMonsterId); return(productMonsterId); }
/// <summary> /// Creates a new product that can be used to test the Collect API. /// </summary> /// <returns>The new product.</returns> public static async Task<ShopifyProduct> CreateProduct() { var service = new ShopifyProductService(Utils.MyShopifyUrl, Utils.AccessToken); var product = new ShopifyProduct() { CreatedAt = DateTime.UtcNow, Title = "Burton Custom Freestlye 151", Vendor = "Burton", BodyHtml = "<strong>Good snowboard!</strong>", ProductType = "Snowboard", Images = new List<ShopifyProductImage> { new ShopifyProductImage { Attachment = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" } }, }; return await service.CreateAsync(product); }
public Product(ShopifyProduct product) { ProductId = product.Id; Name = product.Title; var topVariant = product.Variants.FirstOrDefault(); Price = ConvertToNullDouble(topVariant.Price); InventoryLevel = product.Variants.FirstOrDefault().Inventory_Quantity; Variations = new Variation(); Variations.Size = product.Options.Where(x => x.Name == "Size").FirstOrDefault().Values; Variations.Color = product.Options.Where(x => x.Name == "Color").FirstOrDefault().Values; var weightGrams = ConvertToNullDouble(product.Variants.FirstOrDefault().Grams); Weight = weightGrams == null ? null : weightGrams / 1000; }
/// <summary> /// Creates a new product that can be used to test the Collect API. /// </summary> /// <returns>The new product.</returns> public static async Task <ShopifyProduct> CreateProduct() { var service = new ShopifyProductService(Utils.MyShopifyUrl, Utils.AccessToken); var product = new ShopifyProduct() { CreatedAt = DateTime.UtcNow, Title = "Burton Custom Freestlye 151", Vendor = "Burton", BodyHtml = "<strong>Good snowboard!</strong>", ProductType = "Snowboard", Images = new List <ShopifyProductImage> { new ShopifyProductImage { Attachment = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" } }, }; return(await service.CreateAsync(product)); }
public ShopifyProductModel MakeProductModel(ShopifyProduct input, bool includeVariantGraph = false) { var output = new ShopifyProductModel(); output.ProductTitle = input.ShopifyTitle; output.ProductType = input.ShopifyProductType; output.Vendor = input.ShopifyVendor; output.ShopifyProductId = input.ShopifyProductId; output.VariantCount = input.NonMissingVariants().Count(); output.ShopifyUrl = _shopifyUrlService.ShopifyProductUrl(input.ShopifyProductId); output.SyncedVariantCount = input.NonMissingVariants().Count(x => x.AcumaticaStockItems.Any()); if (includeVariantGraph) { output.Variants = input.NonMissingVariants().Select(x => MakeVariantModel(x)).ToList(); } return(output); }
internal static ShopifyProduct UnpublishProductOnShopify(ShopifyProductService shopifyProductService, long shopifyProductId) { var updateShopifyProduct = default(ShopifyProduct); var shopifyProduct = new ShopifyProduct { Id = shopifyProductId, Published = false }; ShopifyCall.ExecuteCall(delegate() { var task = shopifyProductService.UpdateAsync(shopifyProduct); task.Wait(); updateShopifyProduct = task.Result; }); return(updateShopifyProduct); }
public static ShopifyProduct ChangeShopifyProduct(AliShopifyProduct refProduct, CalcPriceModel[] calculationPrices, ShopifyProduct shopifyProduct, int stockSafetyMargin, string handleFriendly, string title) { var shopifyVariants = default(IEnumerable <ShopifyProductVariant>); var shopifyImages = default(IEnumerable <ShopifyProductImage>); var aliProductSpecificBrandName = refProduct.AliProduct.AliProductSpecific.Where(i => i.Name.ToLower() == "brand name").FirstOrDefault(); var shopsFromCountry = "China"; var shipsFromQuery = refProduct.AliProduct.AliProductOption .Where(i => i.Name.Equals("Ships From", StringComparison.InvariantCultureIgnoreCase)); if (shopifyProduct == null) { shopifyProduct = new ShopifyProduct(); shopifyVariants = new ShopifyProductVariant[0]; shopifyImages = new ShopifyProductImage[0]; } else { shopifyVariants = shopifyProduct.Variants; shopifyImages = shopifyProduct.Images; } shopifyProduct.Title = title; shopifyProduct.Handle = handleFriendly; shopifyProduct.BodyHtml = "<h3 style='margin-top: 25px;'>Item Specifics</h3>"; shopifyProduct.BodyHtml += string.Join("<br />", refProduct.AliProduct.AliProductSpecific .Where(i => i.Type == "item-specifics") .Select(i => $"{Program.ToTitle(i.Name)}: {Program.ToTitle(i.Value)}") ); shopifyProduct.BodyHtml += "<br /><h3 style='margin-top: 15px;'>Packaging Details</h3>"; shopifyProduct.BodyHtml += string.Join("<br />", refProduct.AliProduct.AliProductSpecific .Where(i => i.Type == "packaging-details") .Select(i => $"{Program.ToTitle(i.Name)}: {Program.ToTitle(i.Value)}") ); if (!shipsFromQuery.Any() || refProduct.AliProduct.AliProductOption .Any(i => i.Name.Equals("Ships From", StringComparison.InvariantCultureIgnoreCase) && refProduct.AliProduct.AliProductVariant .Where(v => v.Enabled) .Select(v => i.Number == 1 ? v.Option1 : i.Number == 2 ? v.Option2 : v.Option3) .Contains(shopsFromCountry, StringComparer.InvariantCultureIgnoreCase) )) { if (!refProduct.AliProduct.Enabled && shopifyProduct.PublishedAt != null) { shopifyProduct.Published = false; shopifyProduct.PublishedAt = null; } if (refProduct.AliProduct.Enabled && shopifyProduct.PublishedAt == null) { shopifyProduct.Published = true; shopifyProduct.PublishedAt = DateTime.UtcNow; } } else { shopifyProduct.Published = false; shopifyProduct.PublishedAt = null; } if (aliProductSpecificBrandName == null) { shopifyProduct.Vendor = null; } else { shopifyProduct.Vendor = Program.ToTitle(aliProductSpecificBrandName.Value); } if (shipsFromQuery.Any()) { shopifyProduct.Options = refProduct.AliProduct.AliProductOption .Where(i => !i.Name.Equals("Ships From", StringComparison.InvariantCultureIgnoreCase)) .OrderBy(i => i.Number) .Select(i => new ShopifyProductOption { Name = Program.ToTitle(i.Name), Values = refProduct.AliProduct.AliProductVariant .Where(v => v.Enabled) .Select(v => i.Number == 1 ? v.Option1 : i.Number == 2 ? v.Option2 : v.Option3) .Distinct() }); } else { shopifyProduct.Options = refProduct.AliProduct.AliProductOption .OrderBy(i => i.Number) .Select(i => new ShopifyProductOption { Name = Program.ToTitle(i.Name), Values = refProduct.AliProduct.AliProductVariant .Where(v => v.Enabled) .Select(v => i.Number == 1 ? v.Option1 : i.Number == 2 ? v.Option2 : v.Option3) .Distinct() }); } if (shopifyProduct.Options.Any()) { shopifyProduct.Options = shopifyProduct.Options.ToArray(); var position = 0; foreach (var option in shopifyProduct.Options) { option.Position = ++position; } } else { shopifyProduct.Options = null; } shopifyProduct.Images = refProduct.AliProduct.AliProductImage .Select(i => i.Url) .Distinct() .Select(delegate(string url) { var shopifyImage = GetShopifyImageFromUrl(url, shopifyImages); if (shopifyImage == null) { return(new ShopifyProductImage { Src = url, VariantIds = new long[0] }); } return(new ShopifyProductImage { Id = shopifyImage.Id, VariantIds = new long[0] }); }); shopifyProduct.Variants = refProduct.AliProduct.AliProductVariant .Where(i => i.Enabled && i.AvailableQuantity > 0) .OrderByDescending(i => i.AvailableQuantity) .Select(delegate(AliProductVariant variant) { var skuRefID = variant.AliProductVariantId.ToString(); var shopifyVariant = shopifyVariants.FirstOrDefault(i => i.SKU == skuRefID); var currentPrice = calculationPrices.First(i => i.AliProductVariantId == variant.AliProductVariantId); var shopifyImage = default(ShopifyProductImage); if (variant.AliProductImage != null) { shopifyImage = GetShopifyImageFromUrl(variant.AliProductImage.Url, shopifyImages); } var model = new ShopifyProductVariant { Barcode = skuRefID, FulfillmentService = ShopifyProductFulfillmentService.Manual, InventoryManagement = ShopifyProductInventoryManagement.Shopify, InventoryPolicy = ShopifyProductInventoryPolicy.Deny, ImageId = shopifyImage == null ? null : shopifyImage.Id, Price = (double)currentPrice.CalcPrice, CompareAtPrice = currentPrice.CalcCompareAtPrice == null ? 0 : (double)currentPrice.CalcCompareAtPrice, RequiresShipping = true, SKU = skuRefID, Taxable = false, Title = refProduct.AliProduct.AliProductVariant .Where(i => i.Enabled).Count() - shipsFromQuery.Count() <= 1 ? Program.ToTitle(refProduct.AliProduct.Title) : null, Weight = 0.1, WeightUnit = "kg", }; var shipsFrom = shipsFromQuery.FirstOrDefault(); if (currentPrice.UseDiscount && variant.DiscountPrice.Value > 1.99M || !currentPrice.UseDiscount && variant.OriginalPrice > 1.99M || shipsFrom != null && ( shipsFrom.Number == 1 && !variant.Option1.Equals(shopsFromCountry, StringComparison.InvariantCultureIgnoreCase) || shipsFrom.Number == 2 && !variant.Option2.Equals(shopsFromCountry, StringComparison.InvariantCultureIgnoreCase) || shipsFrom.Number == 3 && !variant.Option3.Equals(shopsFromCountry, StringComparison.InvariantCultureIgnoreCase) )) { return(null); } if (shipsFrom == null) { model.Option1 = Program.ToTitle(variant.Option1); model.Option2 = Program.ToTitle(variant.Option2); model.Option3 = Program.ToTitle(variant.Option3); } else { if (shipsFrom.Number == 1) { model.Option1 = Program.ToTitle(variant.Option2); model.Option2 = Program.ToTitle(variant.Option3); } if (shipsFrom.Number == 2) { model.Option1 = Program.ToTitle(variant.Option1); model.Option2 = Program.ToTitle(variant.Option3); } if (shipsFrom.Number == 3) { model.Option1 = Program.ToTitle(variant.Option1); model.Option2 = Program.ToTitle(variant.Option2); } } if (string.IsNullOrWhiteSpace(model.Option1) || string.IsNullOrWhiteSpace(model.Option2) && ( refProduct.AliProduct.AliProductVariant.GroupBy(i => i.Option1).Any(i => i.Count() > 1) || Regex.IsMatch(model.Option1, "^\\d*$")) ) { model.Option1 = "SKU " + variant.AliProductVariantId; } if (variant.AliProductImageId != null) { model.Option1 += " (As Picture)"; } var stockQuantity = variant.AvailableQuantity.Value - stockSafetyMargin; if (stockQuantity < 0) { stockQuantity = 0; } if (shopifyVariant == null) { model.InventoryQuantity = stockQuantity; } else { model.Id = shopifyVariant.Id; model.Position = shopifyVariant.Position; model.InventoryQuantityAdjustment = stockQuantity - shopifyVariant.InventoryQuantity; } return(model); }); shopifyProduct.Variants = shopifyProduct.Variants .Where(i => i != null) .Take(100) .ToArray(); if (!shopifyProduct.Variants.Any()) { shopifyProduct.Options = null; shopifyProduct.Variants = null; shopifyProduct.Published = false; shopifyProduct.PublishedAt = null; } return(shopifyProduct); }
/// <summary> /// Creates the product. /// </summary> /// <param name="prod">The product.</param> /// <returns></returns> public ShopifyProduct Create(ShopifyProduct prod) { return(base.Create(prod)); }
public static List <ShopifyVariant> NonMissingVariants(this ShopifyProduct product) { return(product.ShopifyVariants.Where(x => !x.IsMissing).ToList()); }
public static string CreatedShopifyProduct(ShopifyProduct product) { return($"Created {product.LogDescriptor()}"); }
public static string DetectedNewProduct(ShopifyProduct product) { return($"Detected new {product.LogDescriptor()}"); }
public static async Task DeleteParentProduct(ShopifyProduct p) { await ProductService.DeleteAsync(p.Id.Value); }
internal static ShopifyProduct UpdateVariantsOnShopify(ShopifyProductService shopifyProductService, AliShopifyProduct refProduct, ShopifyProduct shopifyProduct) { var updateShopifyProduct = default(ShopifyProduct); if (shopifyProduct.Variants.Any()) { shopifyProduct.Variants = shopifyProduct.Variants .Select(delegate(ShopifyProductVariant shopifyVariant) { if (string.IsNullOrWhiteSpace(shopifyVariant.SKU) && shopifyProduct.Variants.Count() == 1) { return(shopifyVariant); } var variant = refProduct.AliProduct.AliProductVariant.FirstOrDefault(i => i.AliProductVariantId.ToString() == shopifyVariant.SKU); if (variant == null || !variant.Enabled) { return(null); } var shopifyImage = default(ShopifyProductImage); if (variant.AliProductImage != null) { shopifyImage = ShopifyHelper.GetShopifyImageFromUrl(variant.AliProductImage.Url, shopifyProduct.Images); } if (shopifyImage != null) { shopifyVariant.ImageId = shopifyImage.Id; } return(shopifyVariant); }) .Where(i => i != null) .ToArray(); ShopifyCall.ExecuteCall(delegate() { var task = shopifyProductService.UpdateAsync(shopifyProduct); task.Wait(); updateShopifyProduct = task.Result; }); } return(updateShopifyProduct ?? shopifyProduct); }
public void InsertProduct(ShopifyProduct product) { Entities.ShopifyProducts.Add(product); Entities.SaveChanges(); }
public CatalogProduct Convert(ShopifyProduct shopifyProduct, ShopifyImportParams importParams, ShopifyData shopifyData, VirtoData virtoData) { var retVal = new CatalogProduct { Id = shopifyProduct.Handle + "-" + shopifyProduct.Id.ToString(), Name = shopifyProduct.Title, Code = shopifyProduct.Handle, StartDate = shopifyProduct.PublishedAt, IsActive = true, CatalogId = importParams.VirtoCatalogId, ProductType = shopifyProduct.ProductType, Vendor = shopifyProduct.Vendor }; //Images if (shopifyProduct.Image != null) { retVal.Images = new List <Image>(); retVal.Images.Add(Convert(shopifyProduct.Image, true)); } //Review if (shopifyProduct.BodyHtml != null) { retVal.Reviews = new List <EditorialReview>(); var review = new EditorialReview { Content = shopifyProduct.BodyHtml, LanguageCode = "en-US", }; retVal.Reviews.Add(review); } //Seo retVal.SeoInfos = new List <SeoInfo>(); var seoInfo = new SeoInfo { SemanticUrl = shopifyProduct.Title.GenerateSlug(), LanguageCode = "en-US" }; retVal.SeoInfos.Add(seoInfo); //TODO: Inventory //Variation if (shopifyProduct.Variants != null) { retVal.Variations = new List <CatalogProduct>(); var isFirst = true; foreach (var shopifyVariant in shopifyProduct.Variants) { var variation = isFirst ? retVal : new CatalogProduct(); variation.Name = String.Format("{0} ({1})", retVal.Name, shopifyVariant.Title); variation.Code = (shopifyProduct.Handle + "-" + shopifyVariant.Title).GenerateSlug(); variation.IsActive = true; variation.SeoInfos = new List <SeoInfo>(); seoInfo = new SeoInfo { SemanticUrl = variation.Name.GenerateSlug(), LanguageCode = "en-US" }; retVal.SeoInfos.Add(seoInfo); if (shopifyVariant.ImageId != null && shopifyProduct.Images != null) { variation.Images = new List <Image>(); var image = shopifyProduct.Images.Where(x => x.Id == shopifyVariant.ImageId).Select(x => Convert(x, true)).FirstOrDefault(); if (image != null) { variation.Images.Add(image); } } //Price variation.Prices = new List <Price>(); variation.Prices.Add(new Price { Sale = shopifyVariant.Price, List = shopifyVariant.Price, Currency = "USD" }); //Properties (need refactor) variation.PropertyValues = new List <PropertyValue>(); var orderedProperties = shopifyProduct.Options.OrderBy(option => option.Position).ToArray(); if (shopifyVariant.Option1 != null) { var propValue = new PropertyValue { PropertyName = orderedProperties[0].Name, Value = shopifyVariant.Option1, ValueType = PropertyValueType.ShortText, }; variation.PropertyValues.Add(propValue); } if (shopifyVariant.Option2 != null) { var propValue = new PropertyValue { PropertyName = orderedProperties[1].Name, Value = shopifyVariant.Option2, ValueType = PropertyValueType.ShortText }; variation.PropertyValues.Add(propValue); } if (shopifyVariant.Option3 != null) { var propValue = new PropertyValue { PropertyName = orderedProperties[2].Name, Value = shopifyVariant.Option3, ValueType = PropertyValueType.ShortText }; variation.PropertyValues.Add(propValue); } if (!isFirst) { retVal.Variations.Add(variation); } isFirst = false; } } if (importParams.ImportCollections) { var firstCollect = shopifyData.Collects.FirstOrDefault(collect => collect.ProductId == shopifyProduct.Id); if (firstCollect != null) { retVal.Category = new Category() { Code = shopifyData.Collections.First(collection => collection.Id == firstCollect.CollectionId) .Handle }; } } return(retVal); }
public ShopifyProduct Update(ShopifyProduct prop) { return(base.Update(prop, prop.Id)); }
public static string LogDescriptor(this ShopifyProduct shopifyProduct) { return($"Shopify Product {shopifyProduct.ShopifyProductId}"); }
internal static ShopifyProduct SaveProductOnShopify(ShopifyProductService shopifyProductService, ShopifyProduct shopifyProduct) { var saveShopifyProduct = default(ShopifyProduct); ShopifyCall.ExecuteCall(delegate() { var task = default(Task <ShopifyProduct>); if (shopifyProduct.Id == null) { task = shopifyProductService.CreateAsync(shopifyProduct); } else { task = shopifyProductService.UpdateAsync(shopifyProduct); } task.Wait(); saveShopifyProduct = task.Result; }); return(saveShopifyProduct); }