New contribution for 3.10 - Import a products variants with unique sku from excel file

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 Jahre weitere
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.
10 Jahre weitere
I am Getting following Error. When i tried to get details of product by clicking on it....
i am using nopcommerce 3.10

Plz Help me as Soon as You Can


We're sorry, an internal error occurred.


Our supporting staff has been notified of this error and will address the issue shortly.

We apologize for the inconvenience.

Please try clicking your browsers 'back' button or try reloading the home page.

If you continue to receive this message, please try again in a little while.

Thank you for your patience.
10 Jahre weitere
(Your issue is not related to this post)

First, check your System > Log

If not obvious, then to find out what went wrong you need to turn off the custom errors mode.
1. Open web.config file
2. Find out the following line <customErrors defaultRedirect="errorpage.htm" mode="RemoteOnly">
3. Replace it with <customErrors defaultRedirect="errorpage.htm" mode="Off">

(then reproduce your actions, and yu will see detailed error messages in browser)
10 Jahre weitere
I already did the changes you suggested in web.config


it is not working


Please Help me out


Please Dear
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.