Created
November 23, 2015 02:20
-
-
Save RyanABailey/95308d50e74d3ed01c5d 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
| /// <summary> | |
| /// Set a custom property on a product | |
| /// </summary> | |
| /// <param name="product">Product</param> | |
| /// <param name="propertyName">Property name</param> | |
| /// <param name="propertyValue">Property value</param> | |
| public static void SetCustomProperty(Product product, string propertyName, string propertyValue) | |
| { | |
| var property = product[propertyName]; | |
| property.Value = propertyValue; | |
| property.Save(); | |
| } | |
| /// <summary> | |
| /// Set a product to a category | |
| /// </summary> | |
| /// <param name="categoryName">category name</param> | |
| /// <param name="product">Product</param> | |
| /// <returns>Action result</returns> | |
| public static bool SetProductCategory(string categoryName, Product product) | |
| { | |
| var category = GetCategory(categoryName); | |
| if (category == null) | |
| { | |
| return false; | |
| } | |
| var relation = new CategoryProductRelation | |
| { | |
| Product = product, | |
| Category = category | |
| }; | |
| try | |
| { | |
| relation.Save(); | |
| } | |
| catch (Exception ex) | |
| { | |
| return false; | |
| } | |
| return true; | |
| } | |
| /// <summary> | |
| /// Hide a product | |
| /// </summary> | |
| /// <param name="product">Product</param> | |
| /// <returns>Action result</returns> | |
| public static bool HideProduct(Product product) | |
| { | |
| if (product != null) | |
| { | |
| if (product.DisplayOnSite) | |
| { | |
| product.DisplayOnSite = false; | |
| product.Save(); | |
| } | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| return true; | |
| } | |
| /// <summary> | |
| /// Get a product from a given category | |
| /// </summary> | |
| /// <param name="category">Category</param> | |
| /// <param name="productName">Product name</param> | |
| /// <returns>Product</returns> | |
| public static Product GetProductFromCategory(Category category, string productName) | |
| { | |
| return category.Products.FirstOrDefault(x => x.Name == productName); | |
| } | |
| /// <summary> | |
| /// Get a variant from a product | |
| /// </summary> | |
| /// <param name="product">Product</param> | |
| /// <param name="variantSku">Variant SKU</param> | |
| /// <returns>Variant product</returns> | |
| public static Product GetVariantFromProduct(Product product, string variantSku) | |
| { | |
| return product.Variants.FirstOrDefault(x => x.VariantSku == variantSku); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment