private SKUTreeNode AppendProduct(TreeNode parent, CampaignProductDto product, SKUInfo sku, int siteId)
        {
            if (parent == null || product == null)
            {
                return(null);
            }

            TreeProvider    tree            = new TreeProvider(MembershipContext.AuthenticatedUser);
            SKUTreeNode     existingProduct = (SKUTreeNode)parent.Children.FirstOrDefault(c => c.NodeSKUID == sku.SKUID);
            SKUTreeNode     newProduct      = existingProduct ?? (SKUTreeNode)TreeNode.New(CampaignsProduct.CLASS_NAME, tree);
            Program         program         = GetProgram(product.Campaign, product.ProgramName);
            ProductCategory productCategory = GetProductCategory(product.ProductCategory);

            newProduct.DocumentName    = product.ProductName;
            newProduct.DocumentSKUName = product.ProductName;
            newProduct.NodeSKUID       = sku.SKUID;
            newProduct.NodeName        = product.ProductName;
            newProduct.DocumentCulture = _culture;
            newProduct.SetValue("ProductName", product.ProductName);
            newProduct.SetValue("BrandID", GetBrandID(product.Brand));
            SetProductItemSpecs(ref newProduct, product);
            if (program != null)
            {
                newProduct.SetValue("ProgramID", program.ProgramID);
            }
            if (!string.IsNullOrWhiteSpace(product.AllowedStates))
            {
                newProduct.SetValue("State", GetStatesGroupID(product.AllowedStates));
            }
            if (!string.IsNullOrWhiteSpace(product.EstimatedPrice))
            {
                newProduct.SetValue("EstimatedPrice", product.EstimatedPrice);
            }
            if (productCategory != null)
            {
                newProduct.SetValue("CategoryID", productCategory.ProductCategoryID);
            }
            if (!string.IsNullOrWhiteSpace(product.BundleQuantity))
            {
                newProduct.SetValue("QtyPerPack", product.BundleQuantity);
            }

            if (existingProduct == null)
            {
                newProduct.Insert(parent);
            }

            if (!string.IsNullOrEmpty(product.ImageURL) && !string.IsNullOrEmpty(product.ThumbnailURL))
            {
                GetAndSaveProductImages(newProduct, product.ImageURL, product.ThumbnailURL);
            }
            else
            {
                RemoveProductImages(newProduct);
            }

            newProduct.Update();

            return(newProduct);
        }
        private void CreateProduct(string productType, DataRow product)
        {
            try
            {
                string productName = ValidationHelper.GetString(product["Name"], string.Empty);

                // Gets a department
                DepartmentInfo department = DepartmentInfoProvider.GetDepartmentInfo(productType + "s", SiteContext.CurrentSiteName);

                // Creates a new product object
                SKUInfo newProduct = new SKUInfo
                {
                    // Sets the product properties
                    SKUName    = productName,
                    SKUNumber  = productName,
                    SKUPrice   = ValidationHelper.GetDecimal(product["Price"], decimal.Zero),
                    SKUEnabled = true
                };
                if (department != null)
                {
                    newProduct.SKUDepartmentID = department.DepartmentID;
                }
                newProduct.SKUSiteID = SiteContext.CurrentSiteID;

                // Saves the product to the database
                // Note: Only creates the SKU object. You also need to create a connected product page to add the product to the site.
                SKUInfoProvider.SetSKUInfo(newProduct);

                // Gets a TreeProvider instance
                TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

                // Gets the parent page
                TreeNode parent = tree.SelectNodes("DancingGoatMvc.ProductSection")
                                  .Path("/Products/" + productType + "s")
                                  .OnCurrentSite()
                                  .FirstOrDefault();

                if ((parent != null) && (newProduct != null))
                {
                    // Creates a new product page of the 'CMS.Product' type
                    SKUTreeNode node = (SKUTreeNode)TreeNode.New("DancingGoatMvc." + productType, tree);

                    // Sets the product page properties
                    node.DocumentSKUName = productName;
                    node.DocumentCulture = LocalizationContext.PreferredCultureCode;

                    // Sets a value for a field of the given product page type
                    //node.SetValue("CoffeeFarm", "Farm 1");
                    //node.SetValue("CoffeeCountry", "India");
                    //node.SetValue("CoffeeVariety", "Criolla, Caturra");
                    //node.SetValue("CoffeeProcessing", "Washed");
                    //node.SetValue("CoffeeAltitude", 4110);

                    node.SetValue("Company", ValidationHelper.GetString(product["Company"], string.Empty));
                    node.SetValue("ModelYear", ValidationHelper.GetString(product["ModelYear"], string.Empty));

                    // Assigns the product to the page
                    node.NodeSKUID = newProduct.SKUID;

                    node.DocumentName = productName;

                    // Saves the product page to the database
                    node.Insert(parent);
                }
            }
            catch (Exception ex)
            {
                EventLogProvider.LogException("CreateProduct", ex.StackTrace, ex);
            }
        }
        private SKUTreeNode AppendProduct(TreeNode parent, ProductDto product, SKUInfo sku, int siteId)
        {
            if (parent == null || product == null)
            {
                return(null);
            }

            TreeProvider tree            = new TreeProvider(MembershipContext.AuthenticatedUser);
            SKUTreeNode  existingProduct = (SKUTreeNode)parent.Children.FirstOrDefault(c => c.NodeSKUID == sku.SKUID);
            SKUTreeNode  newProduct      = existingProduct ?? (SKUTreeNode)TreeNode.New("KDA.Product", tree);

            newProduct.DocumentName    = product.ProductName;
            newProduct.DocumentSKUName = product.ProductName;
            newProduct.NodeSKUID       = sku.SKUID;
            newProduct.NodeName        = product.ProductName;
            newProduct.DocumentCulture = _culture;
            newProduct.SetValue("ProductType", product.ProductType);
            newProduct.SetValue("ProductSKUWeight", Convert.ToDecimal(product.PackageWeight));
            newProduct.SetValue("ProductNumberOfItemsInPackage", Convert.ToInt32(product.ItemsInPackage));
            newProduct.SetValue("ProductChiliTemplateID", product.ChiliTemplateID ?? string.Empty);
            newProduct.SetValue("ProductChiliWorkgroupID", product.ChiliWorkgroupID ?? string.Empty);
            newProduct.SetValue("ProductChiliPdfGeneratorSettingsId", product.ChiliPdfGeneratorSettingsID ?? string.Empty);
            newProduct.SetValue("ProductSKUNeedsShipping", (product.NeedsShipping?.ToLower() ?? string.Empty) == "true");
            newProduct.SetValue("ProductChili3dEnabled", (product.Chili3DEnabled?.ToLower() ?? string.Empty) == "true");
            newProduct.SetValue("ProductDynamicPricing", GetDynamicPricingJson(product.DynamicPriceMinItems, product.DynamicPriceMaxItems, product.DynamicPrice));
            newProduct.SetValue("ProductCustomerReferenceNumber", product.CustomerReferenceNumber);
            newProduct.SetValue("ProductMachineType", product.MachineType);
            newProduct.SetValue("ProductColor", product.Color);
            newProduct.SetValue("ProductPaper", product.Paper);
            newProduct.SetValue("ProductProductionTime", product.ProductionTime);
            newProduct.SetValue("ProductShipTime", product.ShipTime);
            newProduct.SetValue("ProductShippingCost", product.ShippingCost);
            newProduct.SetValue("ProductSheetSize", product.SheetSize);
            newProduct.SetValue("ProductTrimSize", product.TrimSize);
            newProduct.SetValue("ProductFinishedSize", product.FinishedSize);
            newProduct.SetValue("ProductBindery", product.Bindery);

            if (DateTime.TryParse(product.PublishFrom, out DateTime publishFrom))
            {
                newProduct.DocumentPublishFrom = publishFrom;
            }
            if (DateTime.TryParse(product.PublishTo, out DateTime publishTo))
            {
                newProduct.DocumentPublishTo = publishTo;
            }

            SetPageTemplate(newProduct, "_Kadena_Product_Detail");

            if (existingProduct == null)
            {
                newProduct.Insert(parent);
            }

            if (!string.IsNullOrEmpty(product.ImageMediaLibraryName) && !string.IsNullOrEmpty(product.ImagePath))
            {
                AssignImage(newProduct, product.ImageMediaLibraryName, product.ImagePath);
            }
            else
            {
                newProduct.RemoveImage();
            }

            newProduct.Update();

            return(newProduct);
        }