Skip to content

Commit cb9e2f6

Browse files
committed
Testing customer import
1 parent 2c6f0b9 commit cb9e2f6

5 files changed

Lines changed: 64 additions & 38 deletions

File tree

src/Libraries/SmartStore.Services/Catalog/Importer/CategoryImporter.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,14 @@ private void ProcessStoreMappings(IImportExecuteContext context, ImportRow<Categ
239239

240240
private int ProcessCategories(IImportExecuteContext context,
241241
ImportRow<Category>[] batch,
242-
List<int> allCategoryTemplateIds,
242+
Dictionary<string, int> templateViewPaths,
243243
Dictionary<int, ImportCategoryMapping> srcToDestId)
244244
{
245245
_categoryRepository.AutoCommitEnabled = true;
246246

247247
Category lastInserted = null;
248248
Category lastUpdated = null;
249-
var defaultTemplateId = allCategoryTemplateIds.First();
249+
var defaultTemplateId = templateViewPaths["CategoryTemplate.ProductsInGridOrLines"];
250250

251251
foreach (var row in batch)
252252
{
@@ -288,10 +288,7 @@ private int ProcessCategories(IImportExecuteContext context,
288288
continue;
289289
}
290290

291-
category = new Category
292-
{
293-
CategoryTemplateId = defaultTemplateId
294-
};
291+
category = new Category();
295292
}
296293

297294
row.Initialize(category, name);
@@ -321,11 +318,8 @@ private int ProcessCategories(IImportExecuteContext context,
321318
row.SetProperty(context.Result, category, (x) => x.Alias);
322319
row.SetProperty(context.Result, category, (x) => x.DefaultViewMode);
323320

324-
var templateId = row.GetDataValue<int>("CategoryTemplateId");
325-
if (templateId > 0 && allCategoryTemplateIds.Contains(templateId))
326-
{
327-
category.CategoryTemplateId = templateId;
328-
}
321+
var tvp = row.GetDataValue<string>("CategoryTemplateViewPath");
322+
category.CategoryTemplateId = (tvp.HasValue() && templateViewPaths.ContainsKey(tvp) ? templateViewPaths[tvp] : defaultTemplateId);
329323

330324
row.SetProperty(context.Result, category, (x) => x.CreatedOnUtc, UtcNow);
331325
category.UpdatedOnUtc = UtcNow;
@@ -393,9 +387,9 @@ public void Execute(IImportExecuteContext context)
393387
{
394388
var srcToDestId = new Dictionary<int, ImportCategoryMapping>();
395389

396-
var allCategoryTemplateIds = _categoryTemplateService.GetAllCategoryTemplates()
397-
.Select(x => x.Id)
398-
.ToList();
390+
var templateViewPaths = _categoryTemplateService.GetAllCategoryTemplates()
391+
.GroupBy(x => x.ViewPath, StringComparer.OrdinalIgnoreCase)
392+
.ToDictionary(x => x.Key, x => x.First().Id);
399393

400394
using (var scope = new DbContextScope(ctx: _categoryRepository.Context, autoDetectChanges: false, proxyCreation: false, validateOnSave: false))
401395
{
@@ -416,7 +410,7 @@ public void Execute(IImportExecuteContext context)
416410

417411
try
418412
{
419-
ProcessCategories(context, batch, allCategoryTemplateIds, srcToDestId);
413+
ProcessCategories(context, batch, templateViewPaths, srcToDestId);
420414
}
421415
catch (Exception exception)
422416
{

src/Libraries/SmartStore.Services/Catalog/Importer/ProductImporter.cs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class ProductImporter : EntityImporterBase, IEntityImporter
3333
private readonly ICategoryService _categoryService;
3434
private readonly IProductService _productService;
3535
private readonly IUrlRecordService _urlRecordService;
36+
private readonly IProductTemplateService _productTemplateService;
3637
private readonly IStoreMappingService _storeMappingService;
3738
private readonly FileDownloadManager _fileDownloadManager;
3839
private readonly SeoSettings _seoSettings;
@@ -52,6 +53,7 @@ public ProductImporter(
5253
ICategoryService categoryService,
5354
IProductService productService,
5455
IUrlRecordService urlRecordService,
56+
IProductTemplateService productTemplateService,
5557
IStoreMappingService storeMappingService,
5658
FileDownloadManager fileDownloadManager,
5759
SeoSettings seoSettings,
@@ -70,6 +72,7 @@ public ProductImporter(
7072
_categoryService = categoryService;
7173
_productService = productService;
7274
_urlRecordService = urlRecordService;
75+
_productTemplateService = productTemplateService;
7376
_storeMappingService = storeMappingService;
7477
_fileDownloadManager = fileDownloadManager;
7578

@@ -88,7 +91,7 @@ public ProductImporter(
8891
return (int?)null;
8992
}
9093

91-
private int ProcessParentMappings(IImportExecuteContext context,
94+
private int ProcessProductMappings(IImportExecuteContext context,
9295
ImportRow<Product>[] batch,
9396
Dictionary<int, ImportProductMapping> srcToDestId)
9497
{
@@ -527,12 +530,17 @@ private void ProcessStoreMappings(IImportExecuteContext context, ImportRow<Produ
527530
}
528531
}
529532

530-
private int ProcessProducts(IImportExecuteContext context, ImportRow<Product>[] batch, Dictionary<int, ImportProductMapping> srcToDestId)
533+
private int ProcessProducts(
534+
IImportExecuteContext context,
535+
ImportRow<Product>[] batch,
536+
Dictionary<string, int> templateViewPaths,
537+
Dictionary<int, ImportProductMapping> srcToDestId)
531538
{
532539
_productRepository.AutoCommitEnabled = true;
533540

534541
Product lastInserted = null;
535542
Product lastUpdated = null;
543+
var defaultTemplateId = templateViewPaths["ProductTemplate.Simple"];
536544

537545
foreach (var row in batch)
538546
{
@@ -579,7 +587,7 @@ private int ProcessProducts(IImportExecuteContext context, ImportRow<Product>[]
579587
context.Result.AddError("The 'Name' field is required for new products. Skipping row.", row.GetRowInfo(), "Name");
580588
continue;
581589
}
582-
590+
583591
product = new Product();
584592
}
585593

@@ -596,34 +604,39 @@ private int ProcessProducts(IImportExecuteContext context, ImportRow<Product>[]
596604
}
597605
}
598606

599-
row.SetProperty(context.Result, product, (x) => x.Sku);
600-
row.SetProperty(context.Result, product, (x) => x.Gtin);
601-
row.SetProperty(context.Result, product, (x) => x.ManufacturerPartNumber);
602607
row.SetProperty(context.Result, product, (x) => x.ProductTypeId, (int)ProductType.SimpleProduct);
603608
row.SetProperty(context.Result, product, (x) => x.VisibleIndividually, true);
604609
row.SetProperty(context.Result, product, (x) => x.Name);
605610
row.SetProperty(context.Result, product, (x) => x.ShortDescription);
606611
row.SetProperty(context.Result, product, (x) => x.FullDescription);
607-
row.SetProperty(context.Result, product, (x) => x.ProductTemplateId);
612+
row.SetProperty(context.Result, product, (x) => x.AdminComment);
608613
row.SetProperty(context.Result, product, (x) => x.ShowOnHomePage);
609614
row.SetProperty(context.Result, product, (x) => x.HomePageDisplayOrder);
610615
row.SetProperty(context.Result, product, (x) => x.MetaKeywords);
611616
row.SetProperty(context.Result, product, (x) => x.MetaDescription);
612617
row.SetProperty(context.Result, product, (x) => x.MetaTitle);
613618
row.SetProperty(context.Result, product, (x) => x.AllowCustomerReviews, true);
619+
row.SetProperty(context.Result, product, (x) => x.ApprovedRatingSum);
620+
row.SetProperty(context.Result, product, (x) => x.NotApprovedRatingSum);
621+
row.SetProperty(context.Result, product, (x) => x.ApprovedTotalReviews);
622+
row.SetProperty(context.Result, product, (x) => x.NotApprovedTotalReviews);
614623
row.SetProperty(context.Result, product, (x) => x.Published, true);
624+
row.SetProperty(context.Result, product, (x) => x.Sku);
625+
row.SetProperty(context.Result, product, (x) => x.ManufacturerPartNumber);
626+
row.SetProperty(context.Result, product, (x) => x.Gtin);
615627
row.SetProperty(context.Result, product, (x) => x.IsGiftCard);
616628
row.SetProperty(context.Result, product, (x) => x.GiftCardTypeId);
617629
row.SetProperty(context.Result, product, (x) => x.RequireOtherProducts);
618-
row.SetProperty(context.Result, product, (x) => x.RequiredProductIds);
630+
row.SetProperty(context.Result, product, (x) => x.RequiredProductIds); // TODO: global scope
619631
row.SetProperty(context.Result, product, (x) => x.AutomaticallyAddRequiredProducts);
620632
row.SetProperty(context.Result, product, (x) => x.IsDownload);
621633
row.SetProperty(context.Result, product, (x) => x.DownloadId);
622634
row.SetProperty(context.Result, product, (x) => x.UnlimitedDownloads, true);
623635
row.SetProperty(context.Result, product, (x) => x.MaxNumberOfDownloads, 10);
636+
row.SetProperty(context.Result, product, (x) => x.DownloadExpirationDays);
624637
row.SetProperty(context.Result, product, (x) => x.DownloadActivationTypeId, 1);
625638
row.SetProperty(context.Result, product, (x) => x.HasSampleDownload);
626-
row.SetProperty(context.Result, product, (x) => x.SampleDownloadId, (int?)null, ZeroToNull);
639+
row.SetProperty(context.Result, product, (x) => x.SampleDownloadId, (int?)null, ZeroToNull); // TODO: global scope
627640
row.SetProperty(context.Result, product, (x) => x.HasUserAgreement);
628641
row.SetProperty(context.Result, product, (x) => x.UserAgreementText);
629642
row.SetProperty(context.Result, product, (x) => x.IsRecurring);
@@ -635,7 +648,7 @@ private int ProcessProducts(IImportExecuteContext context, ImportRow<Product>[]
635648
row.SetProperty(context.Result, product, (x) => x.AdditionalShippingCharge);
636649
row.SetProperty(context.Result, product, (x) => x.IsEsd);
637650
row.SetProperty(context.Result, product, (x) => x.IsTaxExempt);
638-
row.SetProperty(context.Result, product, (x) => x.TaxCategoryId, 1);
651+
row.SetProperty(context.Result, product, (x) => x.TaxCategoryId, 1); // TODO: global scope
639652
row.SetProperty(context.Result, product, (x) => x.ManageInventoryMethodId);
640653
row.SetProperty(context.Result, product, (x) => x.StockQuantity, 10000);
641654
row.SetProperty(context.Result, product, (x) => x.DisplayStockAvailability);
@@ -661,24 +674,30 @@ private int ProcessProducts(IImportExecuteContext context, ImportRow<Product>[]
661674
row.SetProperty(context.Result, product, (x) => x.CustomerEntersPrice);
662675
row.SetProperty(context.Result, product, (x) => x.MinimumCustomerEnteredPrice);
663676
row.SetProperty(context.Result, product, (x) => x.MaximumCustomerEnteredPrice, 1000);
677+
// HasTierPrices... ignore as long as no tier prices are imported
678+
// LowestAttributeCombinationPrice... ignore as long as no combinations are imported
664679
row.SetProperty(context.Result, product, (x) => x.Weight);
665680
row.SetProperty(context.Result, product, (x) => x.Length);
666681
row.SetProperty(context.Result, product, (x) => x.Width);
667682
row.SetProperty(context.Result, product, (x) => x.Height);
668-
row.SetProperty(context.Result, product, (x) => x.DeliveryTimeId);
669-
row.SetProperty(context.Result, product, (x) => x.QuantityUnitId);
683+
row.SetProperty(context.Result, product, (x) => x.DisplayOrder);
684+
row.SetProperty(context.Result, product, (x) => x.DeliveryTimeId); // TODO: global scope
685+
row.SetProperty(context.Result, product, (x) => x.QuantityUnitId); // TODO: global scope
670686
row.SetProperty(context.Result, product, (x) => x.BasePriceEnabled);
671687
row.SetProperty(context.Result, product, (x) => x.BasePriceMeasureUnit);
672688
row.SetProperty(context.Result, product, (x) => x.BasePriceAmount);
673689
row.SetProperty(context.Result, product, (x) => x.BasePriceBaseAmount);
674-
row.SetProperty(context.Result, product, (x) => x.BundlePerItemPricing);
690+
row.SetProperty(context.Result, product, (x) => x.BundleTitleText);
675691
row.SetProperty(context.Result, product, (x) => x.BundlePerItemShipping);
692+
row.SetProperty(context.Result, product, (x) => x.BundlePerItemPricing);
676693
row.SetProperty(context.Result, product, (x) => x.BundlePerItemShoppingCart);
677-
row.SetProperty(context.Result, product, (x) => x.BundleTitleText);
678694
row.SetProperty(context.Result, product, (x) => x.AvailableStartDateTimeUtc);
679695
row.SetProperty(context.Result, product, (x) => x.AvailableEndDateTimeUtc);
680696
row.SetProperty(context.Result, product, (x) => x.LimitedToStores);
681697

698+
var tvp = row.GetDataValue<string>("ProductTemplateViewPath");
699+
product.ProductTemplateId = (tvp.HasValue() && templateViewPaths.ContainsKey(tvp) ? templateViewPaths[tvp] : defaultTemplateId);
700+
682701
row.SetProperty(context.Result, product, (x) => x.CreatedOnUtc, UtcNow);
683702
product.UpdatedOnUtc = UtcNow;
684703

@@ -745,6 +764,10 @@ public void Execute(IImportExecuteContext context)
745764
{
746765
var srcToDestId = new Dictionary<int, ImportProductMapping>();
747766

767+
var templateViewPaths = _productTemplateService.GetAllProductTemplates()
768+
.GroupBy(x => x.ViewPath, StringComparer.OrdinalIgnoreCase)
769+
.ToDictionary(x => x.Key, x => x.First().Id);
770+
748771
using (var scope = new DbContextScope(ctx: _productRepository.Context, autoDetectChanges: false, proxyCreation: false, validateOnSave: false))
749772
{
750773
var segmenter = context.GetSegmenter<Product>();
@@ -767,7 +790,7 @@ public void Execute(IImportExecuteContext context)
767790
// ===========================================================================
768791
try
769792
{
770-
ProcessProducts(context, batch, srcToDestId);
793+
ProcessProducts(context, batch, templateViewPaths, srcToDestId);
771794
}
772795
catch (Exception exception)
773796
{
@@ -894,7 +917,7 @@ public void Execute(IImportExecuteContext context)
894917

895918
try
896919
{
897-
ProcessParentMappings(context, batch, srcToDestId);
920+
ProcessProductMappings(context, batch, srcToDestId);
898921
}
899922
catch (Exception exception)
900923
{

src/Libraries/SmartStore.Services/Customers/Importer/CustomerImporter.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,15 @@ private void ImportAvatar(IImportExecuteContext context, ImportRow<Customer> row
127127
{
128128
if ((picture = _pictureService.InsertPicture(pictureBinary, image.MimeType, seoName, true, false, false)) != null)
129129
{
130+
_pictureRepository.Context.SaveChanges();
131+
130132
_genericAttributeService.SaveAttribute(row.Entity.Id, SystemCustomerAttributeNames.AvatarPictureId, _attributeKeyGroup, picture.Id.ToString());
131133
}
132134
}
133135
else
134136
{
135137
context.Result.AddInfo("Found equal picture in data store. Skipping field.", row.GetRowInfo(), "AvatarPictureUrl");
136138
}
137-
138139
}
139140
}
140141
else
@@ -143,7 +144,7 @@ private void ImportAvatar(IImportExecuteContext context, ImportRow<Customer> row
143144
}
144145
}
145146

146-
private int ProcessGenericAttributes(IImportExecuteContext context,
147+
private void ProcessGenericAttributes(IImportExecuteContext context,
147148
ImportRow<Customer>[] batch,
148149
List<int> allCountryIds,
149150
List<int> allStateProvinceIds,
@@ -228,11 +229,9 @@ private int ProcessGenericAttributes(IImportExecuteContext context,
228229
{
229230
ImportAvatar(context, row);
230231
}
231-
}
232-
233-
var num = _customerRepository.Context.SaveChanges();
234232

235-
return num;
233+
_services.DbContext.SaveChanges();
234+
}
236235
}
237236

238237
private int ProcessCustomers(IImportExecuteContext context,
@@ -432,12 +431,18 @@ public void Execute(IImportExecuteContext context)
432431

433432
try
434433
{
434+
_services.DbContext.AutoDetectChangesEnabled = true;
435+
435436
ProcessGenericAttributes(context, batch, allCountryIds, allStateProvinceIds, allCustomerNumbers);
436437
}
437438
catch (Exception exception)
438439
{
439440
context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessGenericAttributes");
440441
}
442+
finally
443+
{
444+
_services.DbContext.AutoDetectChangesEnabled = false;
445+
}
441446
}
442447
}
443448
}

src/Libraries/SmartStore.Services/DataExchange/Import/DataImporter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public partial class DataImporter : IDataImporter
6161
private readonly Lazy<IManufacturerService> _manufacturerService;
6262
private readonly Lazy<ICategoryService> _categoryService;
6363
private readonly Lazy<ICategoryTemplateService> _categoryTemplateService;
64+
private readonly Lazy<IProductTemplateService> _productTemplateService;
6465
private readonly Lazy<IProductService> _productService;
6566
private readonly Lazy<IUrlRecordService> _urlRecordService;
6667
private readonly Lazy<IStoreMappingService> _storeMappingService;
@@ -98,6 +99,7 @@ public DataImporter(
9899
Lazy<IManufacturerService> manufacturerService,
99100
Lazy<ICategoryService> categoryService,
100101
Lazy<ICategoryTemplateService> categoryTemplateService,
102+
Lazy<IProductTemplateService> productTemplateService,
101103
Lazy<IProductService> productService,
102104
Lazy<IUrlRecordService> urlRecordService,
103105
Lazy<IStoreMappingService> storeMappingService,
@@ -135,6 +137,7 @@ public DataImporter(
135137
_manufacturerService = manufacturerService;
136138
_categoryService = categoryService;
137139
_categoryTemplateService = categoryTemplateService;
140+
_productTemplateService = productTemplateService;
138141
_productService = productService;
139142
_urlRecordService = urlRecordService;
140143
_storeMappingService = storeMappingService;
@@ -396,6 +399,7 @@ private void ImportCoreOuter(DataImporterContext ctx)
396399
_categoryService.Value,
397400
_productService.Value,
398401
_urlRecordService.Value,
402+
_productTemplateService.Value,
399403
_storeMappingService.Value,
400404
_fileDownloadManager.Value,
401405
_seoSettings.Value,

src/Presentation/SmartStore.Web/Administration/Views/Product/_CreateOrUpdate.AssociatedProducts.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
@if (Model.Id > 0)
66
{
7-
<div style="margin: 10px 0px 10px 0px;" class="alert alert-info">
7+
<div class="alert alert-info">
88
<div>
99
@T("Admin.Catalog.Products.AssociatedProducts.Note1")
1010
@T("Admin.Catalog.Products.AssociatedProducts.Note2")

0 commit comments

Comments
 (0)