Skip to content

Instantly share code, notes, and snippets.

@sitefinitySDK
Created July 17, 2013 12:47
Show Gist options
  • Save sitefinitySDK/6020252 to your computer and use it in GitHub Desktop.
Save sitefinitySDK/6020252 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using Telerik.Sitefinity.Ecommerce.Catalog.Model;
using Telerik.Sitefinity.Modules.Ecommerce.Catalog;
using Telerik.Sitefinity.Modules.Ecommerce.Common;
using Telerik.Sitefinity.Workflow;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.SitefinityEssentials.Modules.Ecommerce.Products
{
public partial class ProductsCodeSnippets
{
public static void CreateLocalizedProduct(string productTypeName, string title, string description, string sku,
double? weight, bool isShippable,
decimal price, DateTime saleEndDate,
DateTime saleStartDate, decimal salePrice, bool isActive, string culture)
{
var cultureInfo = new CultureInfo(culture);
var currentCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
CatalogManager manager = CatalogManager.GetManager();
EcommerceManager ecommerceManager = EcommerceManager.GetManager();
if (manager.GetProducts().Where(x => x.Title == title).SingleOrDefault() != null)
{
return; // Product already exists
}
productTypeName = productTypeName.ToLower();
ProductType productType = ecommerceManager.GetProductTypes().Where(x => x.Title.ToLower() == productTypeName).SingleOrDefault();
if (productType == null)
{
return; // Product Type does not exist
}
Product product = manager.CreateProduct(productType.ClrType);
product.ClrType = productType.ClrType;
product.Title[cultureInfo] = title;
product.Description[cultureInfo] = description;
product.UrlName[cultureInfo] = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
product.AssociateBuyerWithRole = Guid.Empty;
product.DateCreated = DateTime.Now;
product.IsShippable = isShippable;
product.Price = price;
product.IsActive = isActive;
product.SaleEndDate = saleEndDate;
product.SaleStartDate = saleStartDate;
product.SalePrice = salePrice;
product.Sku = sku;
product.Visible = true;
product.Weight = weight;
manager.Provider.RecompileItemUrls(product);
manager.SaveChanges();
var contextBag = new Dictionary<string, string>();
contextBag.Add("ContentType", product.GetType().FullName);
string workflowOperation = "Publish";
WorkflowManager.MessageWorkflow(product.Id,
product.GetType(),
"OpenAccessDataProvider",
workflowOperation,
false,
contextBag);
Thread.CurrentThread.CurrentUICulture = currentCulture;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment