/// <summary> /// Converts image file into product image JSON accepted by SHopify. /// </summary> /// <param name="article">Visma Article object</param> /// <returns>Product JSON string</returns> public string GetProductImageData(Article article, string imageData) { ShopifyProductImage obj = new ShopifyProductImage(); obj.position = 1; obj.filename = string.Format("{0}.jpg", article.ArticleCode); obj.attachment = imageData; List <Metafield> metafields = new List <Metafield>(); if (!string.IsNullOrEmpty(article.ArticleCode)) { Metafield altMeta = new Metafield(); altMeta.@namespace = "tags"; altMeta.key = "alt"; altMeta.value_type = "string"; altMeta.value = article.ArticleCode; metafields.Add(altMeta); } obj.metafields = metafields; // Serialize to Shopify JSON string string data = JsonConvert.SerializeObject(obj, Formatting.Indented); data = "{ \"image\": " + data + "}"; return(data); }
public string GetProductVariantData(Article article, long?variantID, int?pricelistNumber, int?customerNumber, int?quantity, decimal?price, bool taxable, string articleName) { Variant variant = GetProductVariantObject(article, variantID, pricelistNumber, customerNumber, quantity, price, taxable, articleName); // Serialize to Shopify JSON string string data = JsonConvert.SerializeObject(variant, Formatting.Indented); data = "{ \"variant\": " + data + "}"; return(data); }
/// <summary> /// Converts Visma Customer object into Customer JSON accepted by SHopify. /// </summary> /// <param name="article">Visma Customer object</param> /// <returns>Customer JSON string</returns> public string GetCustomerDataFromVismaCustomer(Customer customer, Contact contact, Customer invoiceCustomer, long?addressID) { Customer addressCustomer = (invoiceCustomer == null) ? customer : invoiceCustomer; ShopifyCustomerSimple obj = new ShopifyCustomerSimple(); string name = customer.Name1; string[] nameParts = name.Split(new char[] { ' ' }, 2); string firstName = nameParts[0]; string lastName = (nameParts.Length > 1) ? nameParts[1] : ""; string email = contact.Email; string tags = string.Format("+C{0}", customer.Number); if (customer.PricelistId > 0) { tags += string.Format(", +P{0}", customer.PricelistId); } obj.first_name = firstName; obj.last_name = lastName; obj.email = email; obj.tags = tags; //if (id.HasValue) // obj.id = id.Value; ShopifyAddress[] addresses = new ShopifyAddress[1]; ShopifyAddress adr = new ShopifyAddress(); string[] contactNameParts = contact.Name.Split(new char[] { ' ' }, 2); string contactFirstName = contactNameParts[0]; string contactLastName = (contactNameParts.Length > 1) ? contactNameParts[1] : ""; string[] cityParts = addressCustomer.City.Split(new char[] { ' ' }, 2); string zip = cityParts[0]; string city = (cityParts.Length > 1) ? cityParts[1] : ""; long zipNumber; string phone = (!string.IsNullOrEmpty(contact.Phone)) ? contact.Phone : contact.MobilePhone; if (!long.TryParse(zip, out zipNumber)) { zip = ""; city = addressCustomer.City; } adr.first_name = contactFirstName; adr.last_name = contactLastName; // Save company name if different than conact name if (contact.Name != customer.Name1) { adr.company = customer.Name1; } adr.address1 = addressCustomer.StreetAddress; adr.city = city; adr.zip = zip; if (!string.IsNullOrEmpty(addressCustomer.Country)) { if (addressCustomer.Country.Length == 2) { adr.country_code = addressCustomer.Country; } else { // Try to convert country name to country code string countryName = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(addressCustomer.Country.ToLower()); var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID)); var englishRegion = regions.FirstOrDefault(region => region.EnglishName.Contains(countryName)); if (englishRegion != null) { adr.country_code = englishRegion.TwoLetterISORegionName; } } } adr.phone = phone; adr.@default = true; if ((!string.IsNullOrEmpty(contactFirstName)) && (!string.IsNullOrEmpty(contactLastName))) { obj.first_name = contactFirstName; obj.last_name = contactLastName; } if (addressID.HasValue) { adr.id = addressID.Value; } addresses[0] = adr; obj.default_address = addresses[0]; obj.addresses = addresses; // Serialize to Shopify JSON string string data = JsonConvert.SerializeObject(obj, Formatting.Indented); data = "{ \"customer\": " + data + "}"; return(data); }
/// <summary> /// Converts Visma Article object into Product JSON accepted by SHopify. /// </summary> /// <param name="article">Visma Article object</param> /// <returns>Product JSON string</returns> public string GetProductDataFromVismaArticle(Article article, DateTime?deliveryDate, string articleName, decimal?vatRate, string videoUrl, decimal?pointsObj, bool includeVariants) { // Points from SDK does not work //decimal points = article.Points; decimal points = pointsObj.HasValue ? pointsObj.Value : 0; // Default value 0 - published ShopifyProductSimple obj = new ShopifyProductSimple(); obj.title = string.IsNullOrEmpty(articleName) ? article.ArticleName : articleName; //obj.body_html = ""; obj.product_type = "Product"; obj.published = (points != 2); string tags = string.Format("+T{0}", article.ArticleType); if (points == 1) { tags += ",Order"; } ProductGroup articleProductGroup = null; try { articleProductGroup = article.ProductGroupObject; } catch (System.ArgumentOutOfRangeException ex) { // Note: article.ProductGroup sometimes throws an exception, ignore it. } if ((articleProductGroup != null) && (!string.IsNullOrEmpty(articleProductGroup.Description))) { tags += (string.IsNullOrEmpty(tags)) ? "" : ","; tags += "\"" + article.ProductGroupObject.Description + "\""; } // VAT Rate as tag if (vatRate.HasValue) { tags += string.Format(",+V{0}", vatRate.Value.ToString("0.##")); } // Delivery date as tag if (deliveryDate != null) { tags += string.Format(",+D{0}", deliveryDate.Value.ToString("yyyy-MM-dd")); } obj.tags = tags; // Video URL as metafield List <Metafield> metafields = new List <Metafield>(); if (!string.IsNullOrEmpty(videoUrl)) { Metafield videoMeta = new Metafield(); videoMeta.@namespace = "app"; videoMeta.key = "video"; videoMeta.value_type = "string"; videoMeta.value = videoUrl; metafields.Add(videoMeta); } obj.metafields = metafields; //if (variantID.HasValue) // variant.id = variantID.Value; if (includeVariants) { Variant variant = GetProductVariantObject(article, null, null, null, null, null, true, articleName); Variant variantVat = GetProductVariantObject(article, null, null, null, null, null, false, articleName); List <Variant> variants = new List <Variant> { variant, variantVat }; obj.variants = variants; } // Serialize to Shopify JSON string string data = JsonConvert.SerializeObject(obj, Formatting.Indented); data = "{ \"product\": " + data + "}"; return(data); }