Example #1
0
    /// <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
    /// <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