Exemplo n.º 1
0
        private static dynamic ToDynamic(Manufacturer manufacturer, DataExporterContext ctx)
        {
            if (manufacturer == null)
            {
                return(null);
            }

            dynamic result = new DynamicEntity(manufacturer);

            result.Picture = null;
            result.Name    = ctx.GetTranslation(manufacturer, nameof(manufacturer.Name), manufacturer.Name);

            if (!ctx.IsPreview)
            {
                result.SeName            = ctx.GetUrlRecord(manufacturer);
                result.Description       = ctx.GetTranslation(manufacturer, nameof(manufacturer.Description), manufacturer.Description);
                result.BottomDescription = ctx.GetTranslation(manufacturer, nameof(manufacturer.BottomDescription), manufacturer.BottomDescription);
                result.MetaKeywords      = ctx.GetTranslation(manufacturer, nameof(manufacturer.MetaKeywords), manufacturer.MetaKeywords);
                result.MetaDescription   = ctx.GetTranslation(manufacturer, nameof(manufacturer.MetaDescription), manufacturer.MetaDescription);
                result.MetaTitle         = ctx.GetTranslation(manufacturer, nameof(manufacturer.MetaTitle), manufacturer.MetaTitle);

                result._Localized = GetLocalized(ctx, manufacturer,
                                                 x => x.Name,
                                                 x => x.Description,
                                                 x => x.BottomDescription,
                                                 x => x.MetaKeywords,
                                                 x => x.MetaDescription,
                                                 x => x.MetaTitle);
            }

            return(result);
        }
Exemplo n.º 2
0
        private static dynamic ToDynamic(Category category, DataExporterContext ctx)
        {
            if (category == null)
            {
                return(null);
            }

            dynamic result = new DynamicEntity(category);

            result.Picture  = null;
            result.Name     = ctx.GetTranslation(category, nameof(category.Name), category.Name);
            result.FullName = ctx.GetTranslation(category, nameof(category.FullName), category.FullName);

            if (!ctx.IsPreview)
            {
                result.SeName            = ctx.GetUrlRecord(category);
                result.Description       = ctx.GetTranslation(category, nameof(category.Description), category.Description);
                result.BottomDescription = ctx.GetTranslation(category, nameof(category.BottomDescription), category.BottomDescription);
                result.MetaKeywords      = ctx.GetTranslation(category, nameof(category.MetaKeywords), category.MetaKeywords);
                result.MetaDescription   = ctx.GetTranslation(category, nameof(category.MetaDescription), category.MetaDescription);
                result.MetaTitle         = ctx.GetTranslation(category, nameof(category.MetaTitle), category.MetaTitle);

                result._CategoryTemplateViewPath = ctx.CategoryTemplates.ContainsKey(category.CategoryTemplateId)
                    ? ctx.CategoryTemplates[category.CategoryTemplateId]
                    : "";

                result._Localized = GetLocalized(ctx, category,
                                                 x => x.Name,
                                                 x => x.FullName,
                                                 x => x.Description,
                                                 x => x.BottomDescription,
                                                 x => x.MetaKeywords,
                                                 x => x.MetaDescription,
                                                 x => x.MetaTitle);
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates an expando object with the most important general properties of a product such as the name and description.
        /// This method is used for entities where products are to be exported as related data, e.g. order items.
        /// </summary>
        private async Task <dynamic> ToDynamic(Product product, DataExporterContext ctx, string seName = null, Money?price = null)
        {
            if (product == null)
            {
                return(null);
            }

            dynamic result        = new DynamicEntity(product);
            var     localizedName = ctx.GetTranslation(product, nameof(product.Name), product.Name);

            result.AppliedDiscounts               = null;
            result.Downloads                      = null;
            result.TierPrices                     = null;
            result.ProductAttributes              = null;
            result.ProductAttributeCombinations   = null;
            result.ProductPictures                = null;
            result.ProductCategories              = null;
            result.ProductManufacturers           = null;
            result.ProductTags                    = null;
            result.ProductSpecificationAttributes = null;
            result.ProductBundleItems             = null;
            result.Name = localizedName;

            if (!ctx.IsPreview)
            {
                result.SeName           = seName ?? ctx.GetUrlRecord(product);
                result.ShortDescription = ctx.GetTranslation(product, nameof(product.ShortDescription), product.ShortDescription);
                result.FullDescription  = ctx.GetTranslation(product, nameof(product.FullDescription), product.FullDescription);
                result.MetaKeywords     = ctx.GetTranslation(product, nameof(product.MetaKeywords), product.MetaKeywords);
                result.MetaDescription  = ctx.GetTranslation(product, nameof(product.MetaDescription), product.MetaDescription);
                result.MetaTitle        = ctx.GetTranslation(product, nameof(product.MetaTitle), product.MetaTitle);
                result.BundleTitleText  = ctx.GetTranslation(product, nameof(product.BundleTitleText), product.BundleTitleText);

                result._ProductTemplateViewPath = ctx.ProductTemplates.ContainsKey(product.ProductTemplateId)
                    ? ctx.ProductTemplates[product.ProductTemplateId]
                    : string.Empty;

                if (product.BasePriceHasValue && product.BasePriceAmount != 0)
                {
                    if (price == null)
                    {
                        var calculationOptions = _priceCalculationService.CreateDefaultOptions(false, ctx.ContextCustomer, ctx.ContextCurrency, ctx.ProductBatchContext);
                        var productPrice       = await _priceCalculationService.CalculatePriceAsync(new PriceCalculationContext(product, calculationOptions));

                        price = productPrice.FinalPrice;
                    }

                    result._BasePriceInfo = _priceCalculationService.GetBasePriceInfo(product, price.Value, ctx.ContextCurrency, ctx.ContextLanguage, false);
                }
                else
                {
                    result._BasePriceInfo = string.Empty;
                }

                result.DeliveryTime = ctx.DeliveryTimes.TryGetValue(product.DeliveryTimeId ?? 0, out var deliveryTime)
                    ? ToDynamic(deliveryTime, ctx)
                    : null;

                result.QuantityUnit = ctx.QuantityUnits.TryGetValue(product.QuantityUnitId ?? 0, out var quantityUnit)
                    ? ToDynamic(quantityUnit, ctx)
                    : null;

                result.CountryOfOrigin = product.CountryOfOriginId.HasValue
                    ? ToDynamic(product.CountryOfOrigin, ctx)
                    : null;

                result._Localized = GetLocalized(ctx, product,
                                                 x => x.Name,
                                                 x => x.ShortDescription,
                                                 x => x.FullDescription,
                                                 x => x.MetaKeywords,
                                                 x => x.MetaDescription,
                                                 x => x.MetaTitle,
                                                 x => x.BundleTitleText);
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// The main method to create expando objects for a product to be exported.
        /// Returns several objects if variant combinations are to be exported as products.
        /// </summary>
        private async Task <IEnumerable <dynamic> > Convert(Product product, DataExporterContext ctx)
        {
            var result = new List <dynamic>();
            var seName = ctx.GetUrlRecord(product);

            var productContext = new DynamicProductContext
            {
                SeName             = seName,
                Combinations       = await ctx.ProductBatchContext.AttributeCombinations.GetOrLoadAsync(product.Id),
                AbsoluteProductUrl = await _productUrlHelper.GetAbsoluteProductUrlAsync(product.Id, seName, null, ctx.Store, ctx.ContextLanguage)
            };

            if (ctx.Projection.AttributeCombinationAsProduct && productContext.Combinations.Where(x => x.IsActive).Any())
            {
                if (ctx.Supports(ExportFeatures.UsesAttributeCombinationParent))
                {
                    var dynObject = await ToDynamic(product, true, ctx, productContext);

                    result.Add(dynObject);
                }

                var dbContext = _db as DbContext;

                foreach (var combination in productContext.Combinations.Where(x => x.IsActive))
                {
                    var attachedProduct = _db.Attach(product);
                    product = attachedProduct.Entity;
                    var entry = dbContext.Entry(product);

                    // The returned object is not the entity and is not being tracked by the context.
                    // It also does not have any relationships set to other objects.
                    // CurrentValues only includes database (thus primitive) values.
                    var productClone = entry.CurrentValues.ToObject() as Product;
                    _db.DetachEntity(product);

                    productContext.Combination = combination;

                    var dynObject = await ToDynamic(productClone, false, ctx, productContext);

                    result.Add(dynObject);
                }
            }
            else
            {
                var dynObject = await ToDynamic(product, false, ctx, productContext);

                result.Add(dynObject);
            }

            if (result.Any())
            {
                await _services.EventPublisher.PublishAsync(new RowExportingEvent
                {
                    Row            = result.First(),
                    EntityType     = ExportEntityType.Product,
                    ExportRequest  = ctx.Request,
                    ExecuteContext = ctx.ExecuteContext
                });
            }

            return(result);
        }