Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
        }