public bool DeleteAllProducts(string SubDomain, string APIKey, string APISecret, string APIURL) { try { SetSyncLog(SubDomain, "Deleting Products & Categories (EraLive). Process Started...", true); WooWrapper woo = new WooWrapper(APIKey, APISecret, APIURL); ClassTypes.Products.Product[] AllProducts = woo.GetAllProducts(); ClassTypes.Categories.Category[] AllCategories = woo.GetAllCategories(); SetSyncLog(SubDomain, "Products Found (EraLive): " + AllProducts.Length, true); SetSyncLog(SubDomain, "Categories Found (EraLive): " + AllCategories.Length, true); foreach (ClassTypes.Categories.Category cat in AllCategories) { if (cat.name == "Uncategorized") { continue; } if (woo.DeleteCategory(cat.id.Value) == null) { SetSyncLog(SubDomain, "Error while deleting category (EraLive): " + cat.name + ". Error: " + woo.error, true); } } foreach (ClassTypes.Products.Product prod in AllProducts) { if (woo.DeleteProduct(prod.id.Value) == null) { SetSyncLog(SubDomain, "Error while deleting product (EraLive): " + prod.name + ". Error: " + woo.error, true); } } SetSyncLog(SubDomain, "Products & categories deleted (EraLive)...", true); return(true); } catch (Exception ex) { string errorMessage = "Error: " + ex.Message; if (ex.InnerException != null && ex.InnerException.Message != string.Empty) { errorMessage += "| Inner Exception: " + ex.InnerException.Message; } SetSyncLog(SubDomain, "Error while deleting products & categories (EraLive). " + errorMessage, true); error = ex.Message; return(false); } }
public bool SyncProducts(string DBName, string APIKey, string APISecret, string APIURL) { try { SetSyncLog(DBName, "SyncProducts Process Started...", true); DAL db = new DAL(DBName); DataTable dtProducts = db.execQuery("Get_ProductListForECommerce", System.Data.CommandType.Text, null); DataTable dtCategories = db.execQuery("select distinct Category from Products", System.Data.CommandType.Text, null); SetSyncLog(DBName, "Products Found (EraConnect): " + dtProducts.Rows.Count, true); SetSyncLog(DBName, "Categories Found (EraConnect): " + dtCategories.Rows.Count, true); if (dtProducts != null && dtProducts.Rows.Count > 0) { WooWrapper woo = new WooWrapper(APIKey, APISecret, APIURL); ClassTypes.Products.Product[] AllProducts = woo.GetAllProducts(); ClassTypes.Categories.Category[] AllCategories = woo.GetAllCategories(); SetSyncLog(DBName, "Products Found (EraLive): " + AllProducts.Length, true); SetSyncLog(DBName, "Categories Found (EraLive): " + AllCategories.Length, true); foreach (ClassTypes.Categories.Category cat in AllCategories) { if (cat.name == "Uncategorized") { continue; } bool exists = dtCategories.Select("Category = '" + cat.name + "'").Length > 0; if (exists) { continue; } if (woo.DeleteCategory(cat.id.Value) == null) { SetSyncLog(DBName, "Error while deleting Category (EraLive): " + cat.name + ". Error: " + woo.error, true); } } foreach (DataRow row in dtCategories.Rows) { string CategoryName = row["Category"].ToString(); bool exists = AllCategories.Any(x => x.name == CategoryName); if (!exists) { ClassTypes.Categories.Category newCategory = new ClassTypes.Categories.Category() { name = CategoryName, display = "default" }; if (woo.AddCategory(newCategory) == null) { SetSyncLog(DBName, "Error while adding Category (EraLive): " + newCategory.name + ". Error: " + woo.error, true); } } } AllCategories = woo.GetAllCategories(); foreach (ClassTypes.Products.Product prod in AllProducts) { bool exists = dtProducts.Select("Barcode = '" + prod.sku + "'").Length > 0; if (!exists) { if (woo.DeleteProduct(prod.id.Value) == null) { SetSyncLog(DBName, "Error while Deleting Product (EraLive): " + prod.name + ". Error: " + woo.error, true); } } } foreach (DataRow row in dtProducts.Rows) { string ProductBarcode = row["barcode"].ToString(); bool exists = AllProducts.Any(x => x.sku == ProductBarcode); if (!exists) { ClassTypes.Products.Category[] c = new ClassTypes.Products.Category[1]; c[0] = new ClassTypes.Products.Category(); var MatchedCat = AllCategories.Where(ca => ca.name.ToLower() == Helper.HtmlEncode(row["Category"].ToString().ToLower())).FirstOrDefault(); if (MatchedCat != null) { c[0].id = MatchedCat.id.Value; } ClassTypes.Products.Image[] images = null; //iterate trhough images and add in the above object if (row["Images"].ToString() != string.Empty) { string ImageStoreURL = System.Configuration.ConfigurationManager.AppSettings["ImageStore"]; string[] imgURLs = row["Images"].ToString().Substring(0, row["Images"].ToString().Length - 2).Split(','); images = new ClassTypes.Products.Image[imgURLs.Length]; for (int i = 0; i < imgURLs.Length; i++) { images[i] = new ClassTypes.Products.Image(); images[i].src = string.Concat(ImageStoreURL, imgURLs[i].Trim()); images[i].alt = row["Name"].ToString(); } } ClassTypes.Products.Product newProd = new ClassTypes.Products.Product() { sku = ProductBarcode, name = row["Name"].ToString(), categories = c, short_description = row["Description"].ToString(), regular_price = row["RetailPrice"].ToString(), status = row["Status"].ToString() == "True" ? "publish" : "pending", stock_status = "instock", catalog_visibility = "visible", sale_price = row["SalePrice"].ToString(), date_on_sale_from_gmt = row["PromotionStart"].ToString(), date_on_sale_to_gmt = row["PromotionEnd"].ToString(), images = images, featured = row["Featured"].ToString() == "True" }; if (woo.AddProduct(newProd) == null) { SetSyncLog(DBName, "Error while adding Product (EraLive): " + newProd.name + ". Error: " + woo.error, true); } } else { DateTime ProductModifiedAt = DateTime.Parse(row["ModifiedAt"].ToString()); ClassTypes.Products.Product prod = AllProducts.Where(x => x.sku == ProductBarcode).FirstOrDefault(); if ((prod.date_modified_gmt.HasValue && ProductModifiedAt > prod.date_modified_gmt) || (prod.on_sale.HasValue && prod.on_sale.Value && row["SalePrice"] == null)) { ClassTypes.Products.Category[] c = new ClassTypes.Products.Category[1]; c[0] = new ClassTypes.Products.Category(); var MatchedCat = AllCategories.Where(ca => ca.name.ToLower() == Helper.HtmlEncode(row["Category"].ToString().ToLower())).FirstOrDefault(); if (MatchedCat != null) { c[0].id = MatchedCat.id.Value; } ClassTypes.Products.Image[] images = null; //iterate trhough images and add in the above object if (row["Images"].ToString() != string.Empty) { string ImageStoreURL = System.Configuration.ConfigurationManager.AppSettings["ImageStore"]; string[] imgURLs = row["Images"].ToString().Substring(0, row["Images"].ToString().Length - 2).Split(','); images = new ClassTypes.Products.Image[imgURLs.Length]; for (int i = 0; i < imgURLs.Length; i++) { images[i] = new ClassTypes.Products.Image(); images[i].src = string.Concat(ImageStoreURL, imgURLs[i].Trim()); images[i].alt = row["Name"].ToString(); } } prod.name = row["Name"].ToString(); prod.short_description = row["Description"].ToString(); prod.regular_price = row["RetailPrice"].ToString(); prod.status = row["Status"].ToString() == "True" ? "publish" : "pending"; prod.stock_status = "instock"; prod.categories = c; prod.catalog_visibility = "visible"; prod.images = images; prod.featured = row["Featured"].ToString() == "True"; prod.sale_price = row["SalePrice"].ToString(); prod.date_on_sale_from_gmt = row["PromotionStart"].ToString(); prod.date_on_sale_to_gmt = row["PromotionEnd"].ToString(); } if (woo.UpdateProduct(prod) == null) { SetSyncLog(DBName, "Error while updating Product (EraLive): " + prod.name + ". Error: " + woo.error, true); } } } } SetSyncLog(DBName, "SyncProducts process completed!", true); return(true); } catch (Exception ex) { string errorMessage = "Error: " + ex.Message; if (ex.InnerException != null && ex.InnerException.Message != string.Empty) { errorMessage += "| Inner Exception: " + ex.InnerException.Message; } SetSyncLog(DBName, "Error while Products & Categories Sync. " + errorMessage, true); error = ex.Message; return(false); } }