Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • kapil 3 posts 73 karma points
    Jul 14, 2017 @ 12:33
    kapil
    0

    Merchello create product on fly, not able to render product

    Issue

    Hey, i am developing a customized application using umbraco merchello and fastTrack. Facing an issue in uploading CSV file to merchello products and the issue is product is not rendered when i am click on a specific product from website UI ,which contains Url Slug in href. But it will work after clicking save button in backoffice.

    Code

    var uploadModelRecord = new AlbumProduct();
            var eachValue = value.Split(';');
            var _productService = MerchelloContext.Current.Services.ProductService;
            var _warehouseService = MerchelloContext.Current.Services.WarehouseService;
            var varientService = MerchelloContext.Current.Services.ProductVariantService;
            var entityCollectionService = MerchelloContext.Current.Services.EntityCollectionService;
            var col = entityCollectionService.GetAll().Where(x => x.Name == "Albums").SingleOrDefault();
            var downCol = entityCollectionService.GetAll().Where(x => x.Name == "Downloadable Albums").SingleOrDefault();
            var discCol = entityCollectionService.GetAll().Where(x => x.Name == "Disc Albums").SingleOrDefault();
            var extendedData = new ExtendedDataCollection();
            var warehouse = _warehouseService.GetDefaultWarehouse();
            var catalog = warehouse.WarehouseCatalogs.FirstOrDefault();
            var merchelloServices = MerchelloContext.Current.Services;
            var contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType("ftProduct");
            var detachedContentTypeService = ((ServiceContext)merchelloServices).DetachedContentTypeService;
            var detachedContentType = detachedContentTypeService.GetDetachedContentTypesByContentTypeKey(contentType.Key).FirstOrDefault();
            var existingProduct = _productService.GetAll().Where(x => x.Sku == eachValue[0]).FirstOrDefault();
            IProduct product = null;
            uploadModelRecord.albumCode = eachValue[0];
            uploadModelRecord.albumName = eachValue[1];
            uploadModelRecord.artistName = eachValue[2];
            uploadModelRecord.genres = eachValue[3];
            uploadModelRecord.imageUrl = eachValue[4];
            uploadModelRecord.trackSku = eachValue[5];
            uploadModelRecord.shippable = Convert.ToBoolean(eachValue[6]);
    
            // Calculate number of tracks in each album to fix price
            var noOfTracks = uploadModelRecord.trackSku.Split(',').Count();
            var pricelist = GetPrice();
            var price = pricelist.Select(x => x.Price).ToList();
            double albumPrice = 0;
            if (noOfTracks > 0 && noOfTracks <= 2)
            {
                albumPrice = Math.Round((noOfTracks * price[0]), 2);
            }
            else if (noOfTracks > 2 && noOfTracks <= 5)
            {
                albumPrice = Math.Round((noOfTracks * price[1]), 2);
            }
            else
            {
                albumPrice = Math.Round((noOfTracks * price[2]), 2);
            }
    
            if (existingProduct != null)
            {
                product = existingProduct;
            }
            else
            {
                product = _productService.CreateProduct(uploadModelRecord.albumName, uploadModelRecord.albumCode, (decimal)albumPrice);
            }
    
            product.Available = true;
            product.TrackInventory = false;
            product.Shippable = uploadModelRecord.shippable;
            product.CreateDate = DateTime.UtcNow;
            product.Download = true;
            var opt = product.ProductVariants.Count;
            if (opt == 0)
            {
                product.ProductOptions.Add(new ProductOption("Varient"));
                product.ProductOptions.First(x => x.Name == "Varient").Choices.Add(new ProductAttribute("MP3", "mp3"));
                product.ProductOptions.First(x => x.Name == "Varient").Choices.Add(new ProductAttribute("MP3 + G", "mp3-g"));
                product.ProductOptions.First(x => x.Name == "Varient").Choices.Add(new ProductAttribute("MP4", "mp4"));
            }
            try
            {
                _productService.Save(product, false);
                product.DetachedContents.Add(
                    new ProductVariantDetachedContent(
                        product.ProductVariantKey,
                        detachedContentType,
                        "en-US",
                    new DetachedDataValuesCollection(
                        new[]
                            {
                            new KeyValuePair<string, string>("productName", uploadModelRecord.albumName),
                            new KeyValuePair<string, string>("artistName", uploadModelRecord.artistName),
                            new KeyValuePair<string, string>("genres",uploadModelRecord.genres),
                            new KeyValuePair<string, string>("image",uploadModelRecord.imageUrl),
                            new KeyValuePair<string, string>("tracksSkus",uploadModelRecord.trackSku)
                            }
                        ))
                    {
                        CanBeRendered = true
                    }
                );
                _productService.Save(product, false);
            }
            catch (Exception ex)
            {
                builder.Append("Error:" + uploadModelRecord.albumName + uploadModelRecord.albumCode + "product not uploaded or updated!");
            }
    
            if (catalog == null)
            {
                var error = new NullReferenceException("The default warehouse did not have any catalogs");
                throw error;
            }
            product.AddToCatalogInventory(catalog);
            product.AddToCollection(col.Key);
            _productService.Save(product, false);
            if (uploadModelRecord.shippable)
            {
                product.AddToCollection(discCol.Key);
                _productService.Save(product, false);
            }
            else
            {
                product.AddToCollection(downCol.Key);
                _productService.Save(product, false);
            }
            _productService.Save(product, false);
    

    Please suggest solution as soon as possible.

  • Jorge Sereno 2 posts 72 karma points
    May 20, 2020 @ 11:14
    Jorge Sereno
    0

    Hi Kapil,

    i know its been a while since you post this, but i am facing the very same problem at the moment. I am running out of ideas. Did you found out a solution for this problem?

    Many thankx in advance

Please Sign in or register to post replies

Write your reply to:

Draft