Created
November 23, 2015 02:27
-
-
Save RyanABailey/351334d53229b2951494 to your computer and use it in GitHub Desktop.
uCommerce API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static readonly Guid ProductDefinitionGuid = new Guid("{9B19A3A0-287B-4DA4-B45E-018BD94F2718}"); // Product (template in SC content tree) | |
| private static readonly Guid ProductVariantDefinitionGuid = new Guid("{70D1C14E-15FA-F05C-28D9-2E6DEAAD2A27}"); // Product Variant (template in SC content tree) | |
| private static readonly PriceGroup AuPriceGroup = PriceGroup.SingleOrDefault(x => x.Name == "AU 10 pct"); | |
| // Model to create a product | |
| public class CreateProductModel | |
| { | |
| public string Sku { get; set; } | |
| public string Name { get; set; } | |
| public bool AllowOrdering { get; set; } | |
| public bool DisplayOnSite { get; set; } | |
| public string CategoryName { get; set; } | |
| public decimal? Price { get; set; } | |
| } | |
| // Model to create a product variant | |
| public class CreateProductModel | |
| { | |
| public string Sku { get; set; } | |
| public string Name { get; set; } | |
| public bool AllowOrdering { get; set; } | |
| public bool DisplayOnSite { get; set; } | |
| public string CategoryName { get; set; } | |
| public decimal? Price { get; set; } | |
| public Product ParentProduct { get; set; } | |
| } | |
| /// <summary> | |
| /// Crate a product | |
| /// </summary> | |
| /// <param name="prod">Product model</param> | |
| /// <returns>Product</returns> | |
| public static Product CreateProduct(CreateProductModel prod) | |
| { | |
| // Create product | |
| var product = new Product | |
| { | |
| Sku = prod.Sku, | |
| Name = prod.Name, | |
| AllowOrdering = prod.AllowOrdering, | |
| DisplayOnSite = prod.DisplayOnSite, | |
| ProductDefinition = ProductDefinition.FirstOrDefault(x => x.Guid == ProductDefinitionGuid) | |
| }; | |
| try | |
| { | |
| product.Save(); | |
| } | |
| catch (Exception ex) | |
| { | |
| return null; | |
| } | |
| // set price | |
| var productPrice = new PriceGroupPrice | |
| { | |
| PriceGroup = AuPriceGroup, | |
| Price = prod.Price ?? 0m, | |
| Product = product | |
| }; | |
| productPrice.Save(); | |
| // Set category | |
| var categoryResult = SetProductCategory(prod.CategoryName, product); | |
| return product; | |
| } | |
| /// <summary> | |
| /// Create a product variant | |
| /// </summary> | |
| /// <param name="prod">Product variant model</param> | |
| /// <returns>Product variant</returns> | |
| public static Product CreateProductVariant(CreateProductVariantModel prod) | |
| { | |
| // Create product | |
| var product = new Product | |
| { | |
| Sku = prod.Sku, | |
| VariantSku = prod.Sku, | |
| Name = prod.Name, | |
| AllowOrdering = prod.AllowOrdering, | |
| DisplayOnSite = prod.DisplayOnSite, | |
| ProductDefinition = ProductDefinition.FirstOrDefault(x => x.Guid == ProductVariantDefinitionGuid), | |
| ParentProduct = prod.ParentProduct | |
| }; | |
| try | |
| { | |
| product.Save(); | |
| } | |
| catch (Exception ex) | |
| { | |
| return null; | |
| } | |
| // set price | |
| var productPrice = new PriceGroupPrice | |
| { | |
| PriceGroup = AuPriceGroup, | |
| Price = prod.Price ?? 0m, | |
| Product = product | |
| }; | |
| // Set category | |
| var categoryResult = SetProductCategory(prod.CategoryName, product); | |
| return product; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment