UPDATE: I just found out that nop 3.0 can import product variants as long as both variants of the product have the same SKU.
If the SKU's for each variant are different then you can use the code below.
====
old
====
I'm currently using nopcommerce 3.0 and I noticed the import doesn't work properly when trying to import a product with multiple variants. The problem is that if you export a product with multiple variants and then reimport it will create new products from the variants instead of adding them to the original product.
This code remedies the problem:
importmanager.cs
....
string picture2 = worksheet.Cells[iRow, GetColumnIndex(properties, "Picture2")].Value as string;
string picture3 = worksheet.Cells[iRow, GetColumnIndex(properties, "Picture3")].Value as string;
// NEW CODE OPEN
// get the name of the previous record
string namePrevious = worksheet.Cells[iRow -1, GetColumnIndex(properties, "Name")].Value as string;
// get the sku of the previous record
string skuPrevious = worksheet.Cells[iRow -1, GetColumnIndex(properties, "SKU")].Value as string;
// /NEW CODE CLOSED
var productVariant = _productService.GetProductVariantBySku(sku);
if (productVariant != null)
...
....
_productService.UpdateProductVariant(productVariant);
}
//NEW CODE OPEN
//does the previous record name match the current record name? if so then we know this row is a variant of the previous product.
else if (namePrevious == name)
{
//we need the product id of the product added before this row.
var previousProductVariant = _productService.GetProductVariantBySku(skuPrevious);
//variant
var existingProductAdditionalVariant = new ProductVariant()
{
// product id of previous record
ProductId = previousProductVariant.ProductId,
Name = productVariantName,
Sku = sku,
ManufacturerPartNumber = manufacturerPartNumber,
Gtin = gtin,
IsGiftCard = isGiftCard,
GiftCardTypeId = giftCardTypeId,
RequireOtherProducts = requireOtherProducts,
RequiredProductVariantIds = requiredProductVariantIds,
AutomaticallyAddRequiredProductVariants = automaticallyAddRequiredProductVariants,
IsDownload = isDownload,
DownloadId = downloadId,
UnlimitedDownloads = unlimitedDownloads,
MaxNumberOfDownloads = maxNumberOfDownloads,
DownloadActivationTypeId = downloadActivationTypeId,
HasSampleDownload = hasSampleDownload,
SampleDownloadId = sampleDownloadId,
HasUserAgreement = hasUserAgreement,
UserAgreementText = userAgreementText,
IsRecurring = isRecurring,
RecurringCycleLength = recurringCycleLength,
RecurringCyclePeriodId = recurringCyclePeriodId,
RecurringTotalCycles = recurringTotalCycles,
IsShipEnabled = isShipEnabled,
IsFreeShipping = isFreeShipping,
AdditionalShippingCharge = additionalShippingCharge,
IsTaxExempt = isTaxExempt,
TaxCategoryId = taxCategoryId,
ManageInventoryMethodId = manageInventoryMethodId,
StockQuantity = stockQuantity,
DisplayStockAvailability = displayStockAvailability,
DisplayStockQuantity = displayStockQuantity,
MinStockQuantity = minStockQuantity,
LowStockActivityId = lowStockActivityId,
NotifyAdminForQuantityBelow = notifyAdminForQuantityBelow,
BackorderModeId = backorderModeId,
AllowBackInStockSubscriptions = allowBackInStockSubscriptions,
OrderMinimumQuantity = orderMinimumQuantity,
OrderMaximumQuantity = orderMaximumQuantity,
AllowedQuantities = allowedQuantities,
DisableBuyButton = disableBuyButton,
CallForPrice = callForPrice,
Price = price,
OldPrice = oldPrice,
ProductCost = productCost,
SpecialPrice = specialPrice,
SpecialPriceStartDateTimeUtc = specialPriceStartDateTimeUtc,
SpecialPriceEndDateTimeUtc = specialPriceEndDateTimeUtc,
CustomerEntersPrice = customerEntersPrice,
MinimumCustomerEnteredPrice = minimumCustomerEnteredPrice,
MaximumCustomerEnteredPrice = maximumCustomerEnteredPrice,
Weight = weight,
Length = length,
Width = width,
Height = height,
Published = published,
CreatedOnUtc = createdOnUtc,
UpdatedOnUtc = DateTime.UtcNow
};
_productService.InsertProductVariant(existingProductAdditionalVariant);
//next product
iRow++;
continue;
}
//NEW CODE CLOSED
else
{
//product
var product = new Product()
.....
The code checks to see if the current record has the same name as the previous record, if so then it treats the current record as a variant that belongs to the previous product ID. It then inserts the variant based upon the previous product's id. Thus allowing the import feature to import a product with it's variants.
I hope I didn't do this for nothing. I tried every which way to get the variants imported properly but it just wouldn't work without the above fix.