public List <ProductOption> get_by_product_option_type_id(Int32 id = 0, Int32 languageId = 0) { // Create the list to return List <ProductOption> posts = ProductOption.GetByProductOptionTypeId(id, languageId); // Return the list return(posts); } // End of the get_by_product_option_type_id method
/// <summary> /// Create a PriceRunner file /// </summary> /// <param name="domain">A reference to the domain</param> public static void Create(Domain domain) { // Create the directory path string directoryPath = HttpContext.Current.Server.MapPath("/Content/domains/" + domain.id.ToString() + "/marketing/"); // Check if the directory exists if (System.IO.Directory.Exists(directoryPath) == false) { // Create the directory System.IO.Directory.CreateDirectory(directoryPath); } // Create the filepath string filepath = directoryPath + "PriceRunner.txt"; // Get all data that we need Currency currency = Currency.GetOneById(domain.currency); Int32 decimalMultiplier = (Int32)Math.Pow(10, currency.decimals); Country country = Country.GetOneById(domain.country_id, domain.front_end_language); // Create a stream writer StreamWriter writer = null; try { // Create the file to write UTF-8 encoded text writer = File.CreateText(filepath); // Write the heading for the file writer.WriteLine("Category|Product name|SKU|Price|Shipping Cost|Product URL|Manufacturer SKU|Manufacturer|EAN or UPC|Description|Image URL|Stock Status|Delivery time|Product state|ISBN"); // Get products Int32 page = 1; List<Product> products = Product.GetActiveReliable(domain.front_end_language, 50, page, "title", "ASC"); while(products.Count > 0) { // Loop all the products for (int i = 0; i < products.Count; i++) { // Do not include affiliate products if (products[i].affiliate_link != "") { continue; } // Get all the product options List<ProductOptionType> productOptionTypes = ProductOptionType.GetByProductId(products[i].id, domain.front_end_language); // Check if the product has product options or not if (productOptionTypes.Count > 0) { // Get all the product options Dictionary<Int32, List<ProductOption>> productOptions = new Dictionary<Int32, List<ProductOption>>(productOptionTypes.Count); // Loop all the product option types for (int j = 0; j < productOptionTypes.Count; j++) { List<ProductOption> listProductOptions = ProductOption.GetByProductOptionTypeId(productOptionTypes[j].id, domain.front_end_language); productOptions.Add(j, ProductOption.GetByProductOptionTypeId(productOptionTypes[j].id, domain.front_end_language)); } // Get all the product combinations List<ProductOption[]> productCombinations = new List<ProductOption[]>(); ProductOption.GetProductCombinations(productCombinations, productOptions, 0, new ProductOption[productOptions.Count]); // Loop all the product combinations foreach (ProductOption[] optionArray in productCombinations) { // Get a product copy Product productCopy = products[i].Clone(); // Loop all the product options in the array Int32 optionCounter = 0; foreach (ProductOption option in optionArray) { // Adjust product values productCopy.product_code += option.product_code_suffix; productCopy.manufacturer_code += option.mpn_suffix; productCopy.title += " - " + option.title; productCopy.unit_price += option.price_addition; productCopy.unit_freight += option.freight_addition; productCopy.variant_image_filename = productCopy.variant_image_filename.Replace("[" + optionCounter.ToString() + "]", option.product_code_suffix); // Get the product option type ProductOptionType productOptionType = ProductOptionType.GetOneById(option.product_option_type_id, domain.front_end_language); // Add to the option counter optionCounter++; } // Write the product item to the file WriteProductItem(writer, domain, country, productCopy, currency, decimalMultiplier); } } else { // Write the product item to the file WriteProductItem(writer, domain, country, products[i], currency, decimalMultiplier); } } // Get more products page = page + 1; products = Product.GetActiveReliable(domain.front_end_language, 50, page, "title", "ASC"); } } catch (Exception e) { throw e; } finally { // Close the stream writer if it is different from null if (writer != null) { writer.Close(); } } } // End of the Create method
public ActionResult product(string id = "") { // Get the domain, the product, the currency and the value added tax Domain currentDomain = Tools.GetCurrentDomain(); Product currentProduct = Product.GetOneByPageName(id, currentDomain.front_end_language); // Make sure that the product not is null if (currentProduct == null) { Response.StatusCode = 404; Response.Status = "404 Not Found"; Response.Write(Tools.GetHttpNotFoundPage()); return new EmptyResult(); } // Get additional data Currency currency = Currency.GetOneById(currentDomain.currency); ValueAddedTax valueAddedTax = ValueAddedTax.GetOneById(currentProduct.value_added_tax_id); Category currentCategory = Category.GetOneById(currentProduct.category_id, currentDomain.front_end_language); currentCategory = currentCategory != null ? currentCategory : new Category(); // Get the product price and product code decimal productPrice = currentProduct.unit_price; string productCode = currentProduct.product_code; string manufacturerCode = currentProduct.manufacturer_code; string variantImageUrl = currentProduct.variant_image_filename; // Get product option types and product options List<ProductOptionType> productOptionTypes = ProductOptionType.GetByProductId(currentProduct.id, currentDomain.front_end_language); Dictionary<Int32, List<ProductOption>> productOptions = new Dictionary<int, List<ProductOption>>(productOptionTypes.Count); // Loop all the product option types for (int i = 0; i < productOptionTypes.Count; i++) { List<ProductOption> listProductOptions = ProductOption.GetByProductOptionTypeId(productOptionTypes[i].id, currentDomain.front_end_language); if (listProductOptions.Count > 0) { productPrice += listProductOptions[0].price_addition; productCode += listProductOptions[0].product_code_suffix; manufacturerCode += listProductOptions[0].mpn_suffix; variantImageUrl = variantImageUrl.Replace("[" + i + "]", listProductOptions[0].product_code_suffix); } // Add all the product options for the option type to the dictionary productOptions.Add(productOptionTypes[i].option_type_id, ProductOption.GetByProductOptionTypeId(productOptionTypes[i].id, currentDomain.front_end_language)); } // Adjust the product price with the currency conversion rate productPrice *= (currency.currency_base / currency.conversion_rate); // Round the price to the minor unit for the currency Int32 decimalMultiplier = (Int32)Math.Pow(10, currency.decimals); decimal ordinaryPrice = Math.Round(productPrice * decimalMultiplier, MidpointRounding.AwayFromZero) / decimalMultiplier; productPrice = Math.Round(productPrice * (1 - currentProduct.discount) * decimalMultiplier, MidpointRounding.AwayFromZero) / decimalMultiplier; // Check if prices should include vat bool pricesIncludesVat = Session["PricesIncludesVat"] != null ? Convert.ToBoolean(Session["PricesIncludesVat"]) : currentDomain.prices_includes_vat; // Add vat if prices should include vat if (pricesIncludesVat == true) { ordinaryPrice += Math.Round(ordinaryPrice * valueAddedTax.value * decimalMultiplier, MidpointRounding.AwayFromZero) / decimalMultiplier; productPrice += Math.Round(productPrice * valueAddedTax.value * decimalMultiplier, MidpointRounding.AwayFromZero) / decimalMultiplier; } // Calculate the comparison price decimal comparisonPrice = 0; if (currentProduct.unit_pricing_measure > 0 && currentProduct.unit_pricing_base_measure > 0) { comparisonPrice = (currentProduct.unit_pricing_base_measure / currentProduct.unit_pricing_measure) * productPrice; comparisonPrice = Math.Round(comparisonPrice * decimalMultiplier, MidpointRounding.AwayFromZero) / decimalMultiplier; } // Get the translated texts KeyStringList tt = StaticText.GetAll(currentDomain.front_end_language, "id", "ASC"); // Get a chain of parent categories List<Category> parentCategoryChain = Category.GetParentCategoryChain(currentCategory, currentDomain.front_end_language); // Create the bread crumb list List<BreadCrumb> breadCrumbs = new List<BreadCrumb>(10); breadCrumbs.Add(new BreadCrumb(tt.Get("start_page"), "/")); for (int i = 0; i < parentCategoryChain.Count; i++) { breadCrumbs.Add(new BreadCrumb(parentCategoryChain[i].title, "/home/category/" + parentCategoryChain[i].page_name)); } breadCrumbs.Add(new BreadCrumb(currentProduct.title, "/home/product/" + currentProduct.page_name)); // Update page views if (currentProduct.page_views <= Int32.MaxValue - 1) { Product.UpdatePageviews(currentProduct.id, currentProduct.page_views + 1); } // Get the unit Unit unit = Unit.GetOneById(currentProduct.unit_id, currentDomain.front_end_language); // Set form values ViewBag.BreadCrumbs = breadCrumbs; ViewBag.CurrentDomain = currentDomain; ViewBag.TranslatedTexts = tt; ViewBag.CurrentLanguage = Language.GetOneById(currentDomain.front_end_language); ViewBag.CurrentCategory = currentCategory; ViewBag.Currency = currency; ViewBag.CurrentProduct = currentProduct; ViewBag.ValueAddedTax = valueAddedTax; ViewBag.ProductOptionTypes = productOptionTypes; ViewBag.ProductOptions = productOptions; ViewBag.ProductPrice = productPrice; ViewBag.OrdinaryPrice = ordinaryPrice; ViewBag.ComparisonPrice = comparisonPrice; ViewBag.Unit = unit != null ? unit : new Unit(); ViewBag.ProductCode = productCode; ViewBag.ManufacturerCode = manufacturerCode; ViewBag.VariantImageUrl = variantImageUrl; ViewBag.UserSettings = (Dictionary<string, string>)Session["UserSettings"]; ViewBag.PricesIncludesVat = pricesIncludesVat; ViewBag.CultureInfo = Tools.GetCultureInfo(ViewBag.CurrentLanguage); // Return the view return currentDomain.custom_theme_id == 0 ? View() : View("/Views/theme/product.cshtml"); } // End of the product method
/// <summary> /// Create a google shopping file /// </summary> /// <param name="domain">A reference to the domain</param> public static void Create(Domain domain) { // Create the directory path string directoryPath = HttpContext.Current.Server.MapPath("/Content/domains/" + domain.id.ToString() + "/marketing/"); // Check if the directory exists if (System.IO.Directory.Exists(directoryPath) == false) { // Create the directory System.IO.Directory.CreateDirectory(directoryPath); } // Create the file string filepath = directoryPath + "GoogleShopping.xml.gz"; // Get all data that we need Currency currency = Currency.GetOneById(domain.currency); Int32 decimalMultiplier = (Int32)Math.Pow(10, currency.decimals); Country country = Country.GetOneById(domain.country_id, domain.front_end_language); // Create variables GZipStream gzipStream = null; XmlTextWriter xmlTextWriter = null; try { // Create a gzip stream gzipStream = new GZipStream(new FileStream(filepath, FileMode.Create), CompressionMode.Compress); // Create a xml text writer xmlTextWriter = new XmlTextWriter(gzipStream, new UTF8Encoding(true)); // Set the base url string baseUrl = domain.web_address; // Write the start of the document xmlTextWriter.WriteStartDocument(); // Write the rss tag xmlTextWriter.WriteStartElement("rss"); xmlTextWriter.WriteAttributeString("version", "2.0"); xmlTextWriter.WriteAttributeString("xmlns:g", "http://base.google.com/ns/1.0"); // Write the channel tag xmlTextWriter.WriteStartElement("channel"); // Write information about the channel xmlTextWriter.WriteStartElement("title"); xmlTextWriter.WriteString(domain.webshop_name); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteStartElement("link"); xmlTextWriter.WriteString(baseUrl); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteStartElement("description"); xmlTextWriter.WriteString("Products"); xmlTextWriter.WriteEndElement(); // Get products Int32 page = 1; List <Product> products = Product.GetActiveReliable(domain.front_end_language, 50, page, "title", "ASC"); while (products.Count > 0) { // Loop all the products for (int i = 0; i < products.Count; i++) { // Do not include affiliate products if (products[i].affiliate_link != "") { continue; } // Get all the product options List <ProductOptionType> productOptionTypes = ProductOptionType.GetByProductId(products[i].id, domain.front_end_language); // Check if the product has product options or not if (productOptionTypes.Count > 0) { // Get all the product options Dictionary <Int32, List <ProductOption> > productOptions = new Dictionary <Int32, List <ProductOption> >(productOptionTypes.Count); // Loop all the product option types for (int j = 0; j < productOptionTypes.Count; j++) { List <ProductOption> listProductOptions = ProductOption.GetByProductOptionTypeId(productOptionTypes[j].id, domain.front_end_language); productOptions.Add(j, ProductOption.GetByProductOptionTypeId(productOptionTypes[j].id, domain.front_end_language)); } // Get all the product combinations List <ProductOption[]> productCombinations = new List <ProductOption[]>(); ProductOption.GetProductCombinations(productCombinations, productOptions, 0, new ProductOption[productOptions.Count]); // Loop all the product combinations foreach (ProductOption[] optionArray in productCombinations) { // Get a product copy Product productCopy = products[i].Clone(); // Create an array with google variants List <string[]> googleVariants = new List <string[]>(); // Loop all the product options in the array Int32 optionCounter = 0; foreach (ProductOption option in optionArray) { // Adjust product values productCopy.product_code += option.product_code_suffix; productCopy.manufacturer_code += option.mpn_suffix; productCopy.title += " - " + option.title; productCopy.unit_price += option.price_addition; productCopy.unit_freight += option.freight_addition; productCopy.variant_image_filename = productCopy.variant_image_filename.Replace("[" + optionCounter.ToString() + "]", option.product_code_suffix); // Get the product option type ProductOptionType productOptionType = ProductOptionType.GetOneById(option.product_option_type_id, domain.front_end_language); // Add the google variant if (productOptionType.google_name != "") { googleVariants.Add(new string[] { productOptionType.google_name, option.title }); } // Add to the option counter optionCounter++; } // Write the product item to the xml file WriteProductItem(xmlTextWriter, domain, country, productCopy, currency, decimalMultiplier, googleVariants); } } else { // Write the product item to the xml file WriteProductItem(xmlTextWriter, domain, country, products[i], currency, decimalMultiplier, new List <string[]>(0)); } } // Get more products page = page + 1; products = Product.GetActiveReliable(domain.front_end_language, 50, page, "title", "ASC"); } // Write the end of the document (close rss and channel) xmlTextWriter.WriteEndDocument(); } catch (Exception e) { throw e; } finally { // Close streams if (xmlTextWriter != null) { // Close the XmlTextWriter xmlTextWriter.Close(); } if (gzipStream != null) { // Close the gzip stream gzipStream.Close(); } } } // End of the Create method