Skip to content

Commit bc384c0

Browse files
Add localization of product name, description and full description to export and import
1 parent 226863e commit bc384c0

3 files changed

Lines changed: 143 additions & 8 deletions

File tree

src/Libraries/SmartStore.Services/ExportImport/ExportManager.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using SmartStore.Services.Catalog;
1414
using SmartStore.Services.Common;
1515
using SmartStore.Services.Customers;
16+
using SmartStore.Services.Localization;
1617
using SmartStore.Services.Media;
1718
using SmartStore.Services.Messages;
1819
using SmartStore.Services.Seo;
@@ -33,6 +34,7 @@ public partial class ExportManager : IExportManager
3334
private readonly IProductService _productService;
3435
private readonly IPictureService _pictureService;
3536
private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
37+
private readonly ILanguageService _languageService;
3638

3739
#endregion
3840

@@ -42,13 +44,15 @@ public ExportManager(ICategoryService categoryService,
4244
IManufacturerService manufacturerService,
4345
IProductService productService,
4446
IPictureService pictureService,
45-
INewsLetterSubscriptionService newsLetterSubscriptionService)
47+
INewsLetterSubscriptionService newsLetterSubscriptionService,
48+
ILanguageService languageService)
4649
{
4750
this._categoryService = categoryService;
4851
this._manufacturerService = manufacturerService;
4952
this._productService = productService;
5053
this._pictureService = pictureService;
5154
this._newsLetterSubscriptionService = newsLetterSubscriptionService;
55+
this._languageService = languageService;
5256
}
5357

5458
#endregion
@@ -606,7 +610,7 @@ public virtual void ExportProductsToXlsx(Stream stream, IList<Product> products)
606610
"Picture1",
607611
"Picture2",
608612
"Picture3",
609-
"DeliveryTimeId", // codehint: sm-add (following)
613+
"DeliveryTimeId",
610614
"BasePriceEnabled",
611615
"BasePriceMeasureUnit",
612616
"BasePriceAmount",
@@ -617,8 +621,27 @@ public virtual void ExportProductsToXlsx(Stream stream, IList<Product> products)
617621
"BundlePerItemShoppingCart",
618622
"BundleItemSkus"
619623
};
620-
for (int i = 0; i < properties.Length; i++)
624+
625+
//BEGIN: add headers for languages
626+
var languages = _languageService.GetAllLanguages(true);
627+
var headlines = new string[properties.Length + languages.Count * 3];
628+
var languageFields = new string[languages.Count * 3];
629+
var j = 0;
630+
631+
foreach (var lang in languages)
632+
{
633+
languageFields.SetValue("Name[" + lang.UniqueSeoCode + "]", j++);
634+
languageFields.SetValue("ShortDescription[" + lang.UniqueSeoCode + "]", j++);
635+
languageFields.SetValue("FullDescription[" + lang.UniqueSeoCode + "]", j++);
636+
}
637+
638+
properties.CopyTo(headlines, 0);
639+
languageFields.CopyTo(headlines, properties.Length);
640+
//END: add headers for languages
641+
642+
for (int i = 0; i < headlines.Length; i++)
621643
{
644+
worksheet.Cells[1, i + 1].Value = headlines[i];
622645
cells[1, i + 1].Value = properties[i];
623646
cells[1, i + 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
624647
cells[1, i + 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
@@ -922,6 +945,20 @@ public virtual void ExportProductsToXlsx(Stream stream, IList<Product> products)
922945
cells[row, col].Value = bundleItemSkus;
923946
col++;
924947

948+
//BEGIN: export localized values
949+
foreach (var lang in languages)
950+
{
951+
worksheet.Cells[row, col].Value = p.GetLocalized(x => x.Name, lang.Id, false, false);
952+
col++;
953+
954+
worksheet.Cells[row, col].Value = p.GetLocalized(x => x.ShortDescription, lang.Id, false, false);
955+
col++;
956+
957+
worksheet.Cells[row, col].Value = p.GetLocalized(x => x.FullDescription, lang.Id, false, false); ;
958+
col++;
959+
}
960+
//END: export localized values
961+
925962
row++;
926963
}
927964

src/Libraries/SmartStore.Services/ExportImport/ImportManager.cs

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using SmartStore.Data;
1010
using SmartStore.Services.Catalog;
1111
using SmartStore.Core.Events;
12+
using SmartStore.Services.Localization;
1213
using SmartStore.Services.Media;
1314
using SmartStore.Services.Seo;
1415
using SmartStore.Utilities;
@@ -36,6 +37,8 @@ public partial class ImportManager : IImportManager
3637
private readonly IRepository<Picture> _rsPicture;
3738
private readonly IRepository<ProductPicture> _rsProductPicture;
3839
private readonly IRepository<UrlRecord> _rsUrlRecord;
40+
private readonly ILanguageService _languageService;
41+
private readonly ILocalizedEntityService _localizedEntityService;
3942

4043
public ImportManager(
4144
IProductService productService,
@@ -50,7 +53,9 @@ public ImportManager(
5053
IRepository<ProductManufacturer> rsProductManufacturer,
5154
IRepository<Picture> rsPicture,
5255
IRepository<ProductPicture> rsProductPicture,
53-
IRepository<UrlRecord> rsUrlRecord)
56+
IRepository<UrlRecord> rsUrlRecord,
57+
ILanguageService languageService,
58+
ILocalizedEntityService localizedEntityService)
5459
{
5560
this._productService = productService;
5661
this._categoryService = categoryService;
@@ -65,6 +70,8 @@ public ImportManager(
6570
this._rsProductPicture = rsProductPicture;
6671
this._rsUrlRecord = rsUrlRecord;
6772
this._rsPicture = rsPicture;
73+
this._languageService = languageService;
74+
this._localizedEntityService = localizedEntityService;
6875
}
6976

7077
public virtual string CreateTextReport(ImportResult result)
@@ -199,8 +206,23 @@ public virtual async Task<ImportResult> ImportProductsFromExcelAsync(
199206
_rsProduct.Context.AutoDetectChangesEnabled = false;
200207
}
201208

209+
// ===========================================================================
210+
// 3.) Import Localizations
211+
// ===========================================================================
212+
if (batch.Any(x => x.ContainsKey("CategoryIds")))
213+
{
214+
try
215+
{
216+
await ProcessLocalizations(batch, result);
217+
}
218+
catch (Exception ex)
219+
{
220+
result.AddError(ex, segmenter.CurrentSegment, "ProcessLocalizations");
221+
}
222+
}
223+
202224
// ===========================================================================
203-
// 3.) Import product category mappings
225+
// 4.) Import product category mappings
204226
// ===========================================================================
205227
if (batch.Any(x => x.ContainsKey("CategoryIds")))
206228
{
@@ -215,7 +237,7 @@ public virtual async Task<ImportResult> ImportProductsFromExcelAsync(
215237
}
216238

217239
// ===========================================================================
218-
// 4.) Import product manufacturer mappings
240+
// 5.) Import product manufacturer mappings
219241
// ===========================================================================
220242
if (batch.Any(x => x.ContainsKey("ManufacturerIds")))
221243
{
@@ -230,7 +252,7 @@ public virtual async Task<ImportResult> ImportProductsFromExcelAsync(
230252
}
231253

232254
// ===========================================================================
233-
// 5.) Import product picture mappings
255+
// 6.) Import product picture mappings
234256
// ===========================================================================
235257
if (batch.Any(x => x.ContainsKey("Picture1") || x.ContainsKey("Picture2") || x.ContainsKey("Picture3")))
236258
{
@@ -486,6 +508,79 @@ private async Task<int> ProcessSlugs(ICollection<ImportRow<Product>> batch, Impo
486508
return t;
487509
}
488510

511+
private async Task<int> ProcessLocalizations(ICollection<ImportRow<Product>> batch, ImportResult result)
512+
{
513+
//_rsProductManufacturer.AutoCommitEnabled = false;
514+
515+
//string lastInserted = null;
516+
517+
var languages = _languageService.GetAllLanguages(true);
518+
519+
foreach (var row in batch)
520+
{
521+
522+
Product product = null;
523+
524+
//get product
525+
try
526+
{
527+
object key;
528+
529+
// try get by int ID
530+
if (row.TryGetValue("Id", out key) && key.ToString().ToInt() > 0)
531+
{
532+
product = _productService.GetProductById(key.ToString().ToInt());
533+
}
534+
535+
// try get by SKU
536+
if (product == null && row.TryGetValue("SKU", out key))
537+
{
538+
product = _productService.GetProductBySku(key.ToString());
539+
}
540+
541+
// try get by GTIN
542+
if (product == null && row.TryGetValue("Gtin", out key))
543+
{
544+
product = _productService.GetProductByGtin(key.ToString());
545+
}
546+
}
547+
catch (Exception ex)
548+
{
549+
result.AddWarning(ex.Message, row.GetRowInfo(), "ProcessLocalizations Product");
550+
}
551+
552+
foreach (var lang in languages)
553+
{
554+
string localizedName = row.GetValue<string>("Name[" + lang.UniqueSeoCode + "]");
555+
string localizedShortDescription = row.GetValue<string>("ShortDescription[" + lang.UniqueSeoCode + "]");
556+
string localizedFullDescription = row.GetValue<string>("FullDescription[" + lang.UniqueSeoCode + "]");
557+
558+
if (localizedName.HasValue())
559+
{
560+
_localizedEntityService.SaveLocalizedValue(product, x => x.Name, localizedName, lang.Id);
561+
}
562+
if (localizedShortDescription.HasValue())
563+
{
564+
_localizedEntityService.SaveLocalizedValue(product, x => x.ShortDescription, localizedShortDescription, lang.Id);
565+
}
566+
if (localizedFullDescription.HasValue())
567+
{
568+
_localizedEntityService.SaveLocalizedValue(product, x => x.FullDescription, localizedFullDescription, lang.Id);
569+
}
570+
}
571+
}
572+
573+
// commit whole batch at once
574+
var t = await _rsProductManufacturer.Context.SaveChangesAsync();
575+
576+
// Perf: notify only about LAST insertion and update
577+
//if (lastInserted != null)
578+
// _eventPublisher.EntityInserted(lastInserted);
579+
580+
return t;
581+
}
582+
583+
489584
private async Task<int> ProcessProductCategories(ICollection<ImportRow<Product>> batch, ImportResult result)
490585
{
491586
_rsProductCategory.AutoCommitEnabled = false;

src/Tests/SmartStore.Services.Tests/ExportImport/ExportManagerTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using SmartStore.Core.Domain.Tax;
1313
using SmartStore.Services.Catalog;
1414
using SmartStore.Services.ExportImport;
15+
using SmartStore.Services.Localization;
1516
using SmartStore.Services.Media;
1617
using SmartStore.Services.Messages;
1718
using SmartStore.Tests;
@@ -29,6 +30,7 @@ public class ExportManagerTests : ServiceTest
2930
IPictureService _pictureService;
3031
INewsLetterSubscriptionService _newsLetterSubscriptionService;
3132
IExportManager _exportManager;
33+
ILanguageService _languageService;
3234

3335
[SetUp]
3436
public new void SetUp()
@@ -38,9 +40,10 @@ public class ExportManagerTests : ServiceTest
3840
_productService = MockRepository.GenerateMock<IProductService>();
3941
_pictureService = MockRepository.GenerateMock<IPictureService>();
4042
_newsLetterSubscriptionService = MockRepository.GenerateMock<INewsLetterSubscriptionService>();
43+
_languageService = MockRepository.GenerateMock<ILanguageService>();
4144

4245
_exportManager = new ExportManager(_categoryService,
43-
_manufacturerService, _productService, _pictureService, _newsLetterSubscriptionService);
46+
_manufacturerService, _productService, _pictureService, _newsLetterSubscriptionService, _languageService);
4447
}
4548

4649
//[Test]

0 commit comments

Comments
 (0)