|
using System; |
|
using System.Collections.Generic; |
|
using System.Globalization; |
|
using System.Linq; |
|
using System.Text.RegularExpressions; |
|
using Telerik.Sitefinity.Ecommerce.Catalog.Model; |
|
using Telerik.Sitefinity.Model; |
|
using Telerik.Sitefinity.Modules.Ecommerce.Catalog; |
|
using Telerik.Sitefinity.Modules.Ecommerce.Common; |
|
using Telerik.Sitefinity.Workflow; |
|
|
|
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Multilingual |
|
{ |
|
public partial class MultilingualSnippets |
|
{ |
|
public static Guid CreateLocalizedProduct(Guid productId, string productTypeName, |
|
string title, string description, string sku, double? weight, |
|
bool isShippable, decimal price, DateTime saleEndDate, |
|
DateTime saleStartDate, decimal salePrice, string targetCulture) |
|
{ |
|
CultureInfo culture = CultureInfo.GetCultureInfo(targetCulture); |
|
|
|
CatalogManager manager = CatalogManager.GetManager(); |
|
EcommerceManager ecommerceManager = EcommerceManager.GetManager(); |
|
|
|
if (manager.GetProducts().Where(t => t.Title == title).SingleOrDefault() != null) |
|
{ |
|
return Guid.Empty; // Product already exists |
|
} |
|
|
|
ProductType productType = ecommerceManager.GetProductTypes().Where(t => t.Title == productTypeName).SingleOrDefault(); |
|
if (productType == null) |
|
{ |
|
return Guid.Empty; // Product Type does not exist |
|
} |
|
|
|
Product product = manager.GetProducts().FirstOrDefault(x => x.Id == productId); |
|
if (product == null) |
|
{ |
|
product = manager.CreateProduct(productType.ClrType, productId, manager.Provider.ApplicationName); |
|
} |
|
|
|
product.ClrType = productType.ClrType; |
|
|
|
product.Title.SetString(culture, title); |
|
product.AssociateBuyerWithRole = Guid.Empty; |
|
product.DateCreated = DateTime.Now; |
|
product.GetString("Description").SetString(culture, description); |
|
product.IsShippable = isShippable; |
|
product.Price = price; |
|
product.SaleEndDate = saleEndDate; |
|
product.SaleStartDate = saleStartDate; |
|
product.SalePrice = salePrice; |
|
product.Sku = sku; |
|
product.UrlName.SetString(culture, Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-")); |
|
product.Visible = true; |
|
product.Weight = weight; |
|
|
|
product.Status = Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master; |
|
|
|
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); |
|
|
|
return product.Id; |
|
} |
|
} |
|
} |