public IViewComponentResult Invoke(WidgetInstanceViewModel widgetInstance)
        {
            var model = new ProductWidgetComponentVm
            {
                Id         = widgetInstance.Id,
                WidgetName = widgetInstance.Name,
                Setting    = JsonConvert.DeserializeObject <ProductWidgetSetting>(widgetInstance.Data)
            };

            var query = _productRepository.Query()
                        .Where(x => x.IsPublished && x.IsVisibleIndividually);

            if (model.Setting.CategoryId.HasValue && model.Setting.CategoryId.Value > 0)
            {
                query = query.Where(x => x.Categories.Any(c => c.CategoryId == model.Setting.CategoryId.Value));
            }

            if (model.Setting.FeaturedOnly)
            {
                query = query.Where(x => x.IsFeatured);
            }

            model.Products = query
                             .Include(x => x.ThumbnailImage)
                             .OrderByDescending(x => x.CreatedOn)
                             .Take(model.Setting.NumberOfProducts)
                             .Select(x => ProductThumbnail.FromProduct(x)).ToList();

            foreach (var product in model.Products)
            {
                product.ThumbnailUrl           = _mediaService.GetThumbnailUrl(product.ThumbnailImage);
                product.CalculatedProductPrice = _productPricingService.CalculateProductPrice(product);
            }
            return(View(this.GetViewPath(), model));
        }
Esempio n. 2
0
        public IViewComponentResult Invoke(WidgetInstanceViewModel widgetInstance)
        {
            var model = new ProductWidgetComponentVm
            {
                Id         = widgetInstance.Id,
                WidgetName = _contentLocalizationService.GetLocalizedProperty(nameof(WidgetInstance), widgetInstance.Id, nameof(widgetInstance.Name), widgetInstance.Name),
                Setting    = JsonConvert.DeserializeObject <ProductWidgetSetting>(widgetInstance.Data)
            };

            var query = _productRepository.Query()
                        .Where(x => x.IsPublished && x.IsVisibleIndividually);

            if (model.Setting.CategoryId.HasValue && model.Setting.CategoryId.Value > 0)
            {
                query = query.Where(x => x.Categories.Any(c => c.CategoryId == model.Setting.CategoryId.Value));
            }

            if (model.Setting.FeaturedOnly)
            {
                query = query.Where(x => x.IsFeatured);
            }

            if (model.Setting.OrderBy == ProductWidgetOrderBy.Newest)
            {
                query = query.OrderByDescending(p => p.CreatedOn);
            }

            if (model.Setting.OrderBy == ProductWidgetOrderBy.BestSelling)
            {
                //TODO: ProductWidgetOrderBy.BestSelling must be managed
                // create a new  property in product incremented each time a product
                // is created or updated or calculated how many time the products already
                // ordered ?
            }

            var productThumbnail = query
                                   .Include(x => x.ThumbnailImage)
                                   .Select(x => ProductThumbnail.FromProduct(x));

            if (model.Setting.OrderBy == ProductWidgetOrderBy.Discount)
            {
                model.Products = model.Products.OrderByDescending(p => p.CalculatedProductPrice.PercentOfSaving).ToList();
            }



            model.Products = productThumbnail.Take(model.Setting.NumberOfProducts).ToList();

            foreach (var product in model.Products)
            {
                product.Name                   = _contentLocalizationService.GetLocalizedProperty(nameof(Product), product.Id, nameof(product.Name), product.Name);
                product.ThumbnailUrl           = _mediaService.GetThumbnailUrl(product.ThumbnailImage);
                product.CalculatedProductPrice = _productPricingService.CalculateProductPrice(product);
            }

            return(View(this.GetViewPath(), model));
        }
Esempio n. 3
0
        public IActionResult Collection()
        {
            var widgetInstances = _widgetInstanceRepository.Query()
                                  .Where(x => x.WidgetId == WidgetIds.ProductWidget)
                                  .OrderBy(x => x.DisplayOrder)
                                  .Select(x => new
            {
                Id         = x.Id,
                WidgetName = x.Name,
                Setting    = JsonConvert.DeserializeObject <ProductWidgetSetting>(x.Data)
            })
                                  .ToList();

            var productWidgets = new List <ProductWidgetComponentVm>();

            foreach (var item in widgetInstances)
            {
                var model = new ProductWidgetComponentVm
                {
                    Id         = item.Id,
                    WidgetName = item.WidgetName,
                    Setting    = item.Setting
                };

                var query = _productRepository.Query()
                            .Where(x => x.IsPublished && x.IsVisibleIndividually);

                if (model.Setting.CategoryId.HasValue && model.Setting.CategoryId.Value > 0)
                {
                    query = query.Where(x => x.Categories.Any(c => c.CategoryId == model.Setting.CategoryId.Value));
                }

                if (model.Setting.FeaturedOnly)
                {
                    query = query.Where(x => x.IsFeatured);
                }

                model.Products = query
                                 .Include(x => x.ThumbnailImage)
                                 .OrderByDescending(x => x.CreatedOn)
                                 .Take(model.Setting.NumberOfProducts)
                                 .Select(x => ProductThumbnail.FromProduct(x)).ToList();

                foreach (var product in model.Products)
                {
                    product.ThumbnailUrl           = _mediaService.GetThumbnailUrl(product.ThumbnailImage);
                    product.CalculatedProductPrice = _productPricingService.CalculateProductPrice(product);
                }

                productWidgets.Add(model);
            }
            ;
            return(Json(productWidgets));
        }
        public IViewComponentResult Invoke(WidgetInstanceViewModel widgetInstance)
        {
            var model = new ProductWidgetComponentVm
            {
                Id         = widgetInstance.Id,
                WidgetName = widgetInstance.Name,
                Setting    = JsonConvert.DeserializeObject <ProductWidgetSetting>(widgetInstance.Data)
            };

            var query = _productRepository.Query()
                        .Where(x => x.IsPublished && x.IsVisibleIndividually);

            if (model.Setting.FeaturedOnly)
            {
                query = query.Where(x => x.IsFeatured);
            }

            model.Products = query
                             .OrderByDescending(x => x.CreatedOn)
                             .Take(model.Setting.NumberOfProducts)
                             .Select(x => new ProductThumbnail
            {
                Id                = x.Id,
                Name              = x.Name,
                SeoTitle          = x.SeoTitle,
                Price             = x.Price,
                OldPrice          = x.OldPrice,
                SpecialPrice      = x.SpecialPrice,
                SpecialPriceStart = x.SpecialPriceStart,
                SpecialPriceEnd   = x.SpecialPriceEnd,
                StockQuantity     = x.StockQuantity,
                IsAllowToOrder    = x.IsAllowToOrder,
                IsCallForPricing  = x.IsCallForPricing,
                ThumbnailImage    = x.ThumbnailImage,
                NumberVariation   = x.ProductLinks.Count,
                ReviewsCount      = x.ReviewsCount,
                RatingAverage     = x.RatingAverage
            }).ToList();

            foreach (var product in model.Products)
            {
                product.ThumbnailUrl           = _mediaService.GetThumbnailUrl(product.ThumbnailImage);
                product.CalculatedProductPrice = _productPricingService.CalculateProductPrice(product);
            }

            return(View("/Modules/SimplCommerce.Module.Catalog/Views/Components/ProductWidget.cshtml", model));
        }
Esempio n. 5
0
        public IViewComponentResult Invoke(WidgetInstanceViewModel widgetInstance)
        {
            //cambiamos aqui, tenemos que anaizar de donde vienen los widgets para poder diferenciar que cargar y donde.
            // se definene en la parte de admnistracion
            var productos = RecArticle(GetIP(), GetSession());
            //var model = new ProductWidgetComponentVm
            //{
            //    Id = widgetInstance.Id,
            //    WidgetName = widgetInstance.Name,
            //    Setting = JsonConvert.DeserializeObject<ProductWidgetSetting>(widgetInstance.Data)
            //};



            var model = new ProductWidgetComponentVm
            {
                Id         = widgetInstance.Id,
                WidgetName = widgetInstance.Name,
                Setting    = JsonConvert.DeserializeObject <ProductWidgetSetting>(widgetInstance.Data)
            };

            model.Products = new List <ProductThumbnail>();

            //CODIGO ORIGEN
            //var query = _productRepository.Query()
            //  .Where(x => x.IsPublished && x.IsVisibleIndividually);

            //if (model.Setting.CategoryId.HasValue && model.Setting.CategoryId.Value > 0)
            //{
            //    query = query.Where(x => x.Categories.Any(c => c.CategoryId == model.Setting.CategoryId.Value));
            //}

            //if (model.Setting.FeaturedOnly)
            //{
            //    query = query.Where(x => x.IsFeatured);
            //}

            //model.Products = query
            //  .Include(x => x.ThumbnailImage)
            //  .OrderByDescending(x => x.CreatedOn)
            //  .Take(model.Setting.NumberOfProducts)
            //  .Select(x => ProductThumbnail.FromProduct(x)).ToList();
            //foreach (var product in model.Products)
            //{
            //    product.Name = _contentLocalizationService.GetLocalizedProperty(nameof(Product), product.Id, nameof(product.Name), product.Name);
            //    product.ThumbnailUrl = _mediaService.GetThumbnailUrl(product.ThumbnailImage);
            //    product.CalculatedProductPrice = _productPricingService.CalculateProductPrice(product);
            //}

            //FIN CODIGO ORIGEN
            foreach (producto p in productos.Result.result)
            {
                ViewModels.ProductThumbnail tm = new ProductThumbnail();
                tm.Id           = long.Parse(p.identifier);
                tm.Name         = p.description;
                tm.ThumbnailUrl = p.imagelarge;
                int r = 0;
                _ = int.TryParse(p.stocks, out r);
                tm.StockQuantity = r;
                decimal pr = 0;
                pr                = decimal.Parse(p.pricewithtax.Replace(".", ","));
                tm.Price          = pr;
                tm.ReviewsCount   = int.Parse(p.likeothers);
                tm.IsAllowToOrder = true;
                tm.Slug           = tm.Name.Replace(" ", "-") + "-" + tm.Id;
                Core.Models.Media pti = new ProductThumbnail().ThumbnailImage;
                tm.ThumbnailUrl = _mediaService.GetThumbnailUrl(pti);
                tm.ThumbnailUrl = _mediaService.GetURL(p.imagelarge);

                //tm.CalculatedProductPrice(p);
                //tm.CalculatedProductPrice = _productPricingService.CalculateProductPrice((decimal.Parse(p.pricewithtax)));
                tm.CalculatedProductPrice = _productPricingService.CalculateProductPrice(tm);
                model.Products.Add(tm);
                //añadimos a la tabla slug si no existe
                var entity = _entityRepository
                             .Query()
                             .Include(x => x.EntityType)
                             .FirstOrDefault(x => x.Slug == tm.Slug);
                if (entity == null)
                {
                    Entity en = new Entity();

                    en.EntityId = (long)tm.Id;
                    en.Name     = tm.Name;
                    en.Slug     = tm.Slug;
                    var enType = _entityTypeRepository.Query().FirstOrDefault(x => x.Id == "Product");
                    en.EntityType = enType;

                    //en.EntityType = (EntityType)enType;
                    //en.EntityType = new EntityType("Product");
                    //en.EntityType.AreaName = "Catalog";
                    //en.EntityType.IsMenuable = false;
                    //en.EntityType.RoutingController = "Product";
                    //en.EntityType.RoutingAction = "ProductDetail";
                    _entityRepository.Add(en);
                    _entityRepository.SaveChanges();
                }
            }

            return(View(this.GetViewPath(), model));
        }