function foxyshop_create_feed()
{
    global $product, $google_product_field_names;
    $amazon_version = isset($_GET['amazon_version']) ? 1 : 0;
    //Field Names
    $fieldnames = array('id', 'item_group_id', 'product_type', 'title', 'link', 'price', 'sale_price', 'sale_price_effective_date', 'availability', 'description', 'image_link', 'additional_image_link');
    $fieldnames = array_merge($fieldnames, $google_product_field_names);
    $lastfieldname = end($fieldnames);
    $write = "";
    foreach ($fieldnames as $field) {
        if ($field != $fieldnames[0]) {
            $write .= "\t";
        }
        if ($amazon_version) {
            if ($field == "product_type") {
                $field = "category";
            }
            if ($field == "id") {
                $field = "sku";
            }
            if ($field == "image_link") {
                $field = "image";
            }
            if ($field == "Mfr part number") {
                $field = "mpn";
            }
        }
        $write .= '"' . $field . '"';
    }
    $write .= "\n";
    $products = get_posts(array('post_type' => 'foxyshop_product', 'post_status' => "publish", 'posts_per_page' => (int) $_GET['records_per_page'], 'paged' => (int) $_GET['records_page']));
    foreach ($products as $singleproduct) {
        $product = foxyshop_setup_product($singleproduct);
        $product = apply_filters('foxyshop_setup_product_info_google', $product);
        foreach ($fieldnames as $fieldname) {
            $write .= '"';
            switch ($fieldname) {
                case "id":
                    $write .= foxyshop_dblquotes($product['id']);
                    break;
                case "title":
                    $write .= foxyshop_dblquotes($product['name']);
                    break;
                case "link":
                    $write .= foxyshop_dblquotes($product['url']);
                    break;
                case "availability":
                    $availability = get_post_meta($product['id'], '_availability', 1);
                    if (!$availability) {
                        $availability = "in stock";
                    }
                    $write .= foxyshop_dblquotes($availability);
                    break;
                case "price":
                    $write .= foxyshop_dblquotes($product['originalprice'] . " " . apply_filters("foxyshop_google_product_currency", "USD"));
                    break;
                case "sale_price":
                    $write .= foxyshop_dblquotes($product['originalprice'] != $product['price'] ? $product['price'] . " " . apply_filters("foxyshop_google_product_currency", "USD") : '');
                    break;
                case "sale_price_effective_date":
                    if ($product['originalprice'] != $product['price']) {
                        $salestartdate = get_post_meta($product['id'], '_salestartdate', 1);
                        $saleenddate = get_post_meta($product['id'], '_saleenddate', 1);
                        if ($salestartdate == '999999999999999999') {
                            $salestartdate = 0;
                        }
                        if ($saleenddate == '999999999999999999') {
                            $saleenddate = 0;
                        }
                        $salestartdate = $salestartdate == 0 ? Date("Y-m-d", strtotime("-1 day")) : Date("Y-m-d", $salestartdate);
                        $saleenddate = $saleenddate == 0 ? Date("Y-m-d", strtotime("+1 year")) : Date("Y-m-d", $saleenddate);
                        $write .= foxyshop_dblquotes($salestartdate . "/" . $saleenddate);
                    } else {
                        $write .= foxyshop_dblquotes('');
                    }
                    break;
                case "description":
                    $write .= foxyshop_dblquotes(strip_tags($product['description']));
                    break;
                case "product_type":
                    $product_type_write = "";
                    $categories = wp_get_post_terms($product['id'], 'foxyshop_categories');
                    foreach ($categories as $cat) {
                        if ($product_type_write) {
                            $product_type_write .= "\n";
                        }
                        $breadcrumbarray = array_reverse(get_ancestors($cat->term_id, 'foxyshop_categories'));
                        foreach ($breadcrumbarray as $crumb) {
                            $term = get_term_by('id', $crumb, 'foxyshop_categories');
                            $product_type_write .= $term->name . ' > ';
                        }
                        $product_type_write .= $cat->name;
                    }
                    $write .= foxyshop_dblquotes($product_type_write);
                    break;
                case "condition":
                    $condition = get_post_meta($product['id'], '_condition', 1);
                    if (!$condition) {
                        $condition = "new";
                    }
                    $write .= foxyshop_dblquotes($condition);
                    break;
                case 'gtin':
                    $gtin = get_post_meta($product['id'], '_gtin', 1);
                    if (!$gtin) {
                        $gtin = $product['code'];
                    }
                    $write .= foxyshop_dblquotes($gtin);
                    break;
                case 'mpn':
                    $mpn = get_post_meta($product['id'], '_mpn', 1);
                    if (!$mpn) {
                        $mpn = $product['code'];
                    }
                    $write .= foxyshop_dblquotes($mpn);
                    break;
                case "image_link":
                    $write .= foxyshop_dblquotes(foxyshop_get_main_image(apply_filters("foxyshop_google_product_image_size", "thumbnail")));
                    break;
                case "additional_image_link":
                    $additional_images = array();
                    $number_of_additional_images = 0;
                    foreach ($product['images'] as $product_image) {
                        $number_of_additional_images++;
                        if ($product_image['featured'] == 0 && $number_of_additional_images <= 10) {
                            $additional_images[] = $product_image[apply_filters("foxyshop_google_product_image_size", "thumbnail")];
                        }
                    }
                    $write .= foxyshop_dblquotes(implode(",", $additional_images));
                    break;
                default:
                    $write .= foxyshop_dblquotes(get_post_meta($product['id'], "_" . $fieldname, 1));
                    break;
            }
            $write .= '"';
            if ($fieldname != $lastfieldname) {
                $write .= "\t";
            }
        }
        $write .= "\n";
    }
    echo $write;
}
function foxyshop_transaction_export()
{
    global $foxyshop_settings;
    if ($_GET['transaction_search_type'] == "export_csv") {
        $field_delimiter = ",";
    } elseif ($_GET['transaction_search_type'] == "export_tab") {
        $field_delimiter = "\t";
    }
    //Setup Fields and Defaults
    $foxy_data_defaults = array("is_test_filter" => "0", "hide_transaction_filter" => "0", "data_is_fed_filter" => "", "id_filter" => "", "order_total_filter" => "", "coupon_code_filter" => "", "transaction_date_filter_begin" => date("Y-m-d", strtotime("-10 days")), "transaction_date_filter_end" => date("Y-m-d"), "customer_id_filter" => "", "customer_email_filter" => "", "customer_first_name_filter" => "", "customer_last_name_filter" => "", "customer_state_filter" => "", "shipping_state_filter" => "", "customer_ip_filter" => "", "product_code_filter" => "", "product_name_filter" => "", "product_option_name_filter" => "", "product_option_value_filter" => "");
    if (version_compare($foxyshop_settings['version'], '0.7.2', ">=")) {
        $foxy_data_defaults["custom_field_name_filter"] = "";
        $foxy_data_defaults["custom_field_value_filter"] = "";
    }
    $foxy_data = wp_parse_args(array("api_action" => "transaction_list"), $foxy_data_defaults);
    if (isset($_GET['foxyshop_search'])) {
        $fields = array("is_test_filter", "hide_transaction_filter", "data_is_fed_filter", "id_filter", "order_total_filter", "coupon_code_filter", "transaction_date_filter_begin", "transaction_date_filter_end", "customer_id_filter", "customer_email_filter", "customer_first_name_filter", "customer_last_name_filter", "customer_state_filter", "shipping_state_filter", "customer_ip_filter", "product_code_filter", "product_name_filter", "product_option_name_filter", "product_option_value_filter", "custom_field_name_filter", "custom_field_value_filter");
        foreach ($fields as $field) {
            if (isset($_GET[$field])) {
                $foxy_data[$field] = $_GET[$field];
            }
        }
        $foxy_data['pagination_start'] = isset($_GET['pagination_start']) ? $_GET['pagination_start'] : 0;
        if ($foxyshop_settings['version'] != "0.7.0") {
            $foxy_data['entries_per_page'] = 10000;
        }
    }
    $foxy_response = foxyshop_get_foxycart_data($foxy_data);
    $xml_return = simplexml_load_string($foxy_response, NULL, LIBXML_NOCDATA);
    if ((string) $xml_return->result == "ERROR") {
        echo '<h3>' . $xml_return->messages->message . '</h3>';
        die;
    }
    // Define the path to file
    $filename = 'foxycart-export-' . Date('d-m-Y') . '.' . ($field_delimiter == "," ? "csv" : "txt");
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
    header("Content-Type: text/csv");
    $fields = array('transaction_id', 'store_id', 'transaction_date', 'product_total', 'tax_total', 'shipping_total', 'discount_total', 'order_total', 'purchase_order', 'cc_type', 'cc_number_masked', 'cc_exp_month', 'cc_exp_year', 'processor_response', 'customer_id', 'is_anonymous', 'minfraud_score', 'customer_first_name', 'customer_last_name', 'customer_company', 'customer_address1', 'customer_address2', 'customer_city', 'customer_state', 'customer_postal_code', 'customer_country', 'customer_phone', 'customer_email', 'customer_ip', 'custom_fields', 'attributes', 'coupons_used', 'shipping_service_description', 'shipping_first_name', 'shipping_last_name', 'shipping_company', 'shipping_address1', 'shipping_address2', 'shipping_city', 'shipping_state', 'shipping_postal_code', 'shipping_country', 'shipping_phone', 'sub_token_url', 'category_code', 'product_name', 'product_code', 'product_price', 'product_quantity', 'product_weight', 'product_options');
    echo implode($field_delimiter, $fields) . "\n";
    foreach ($xml_return->transactions->transaction as $transaction) {
        $custom_fields = "";
        if (!empty($transaction->custom_fields)) {
            foreach ($transaction->custom_fields->custom_field as $custom_field) {
                if ($custom_fields) {
                    $custom_fields .= " - ";
                }
                $custom_fields .= (string) $custom_field->custom_field_name . ":" . (string) $custom_field->custom_field_value;
            }
        }
        $attributes1 = "";
        if (!empty($transaction->attributes)) {
            foreach ($transaction->attributes->attribute as $attribute) {
                if ($attributes1) {
                    $attributes1 .= " - ";
                }
                $attributes1 .= (string) $attribute->name . ":" . (string) $attribute->value;
            }
        }
        $discounts = "";
        $discount_total = 0;
        if (!empty($transaction->discounts)) {
            foreach ($transaction->discounts->discount as $discount) {
                if ($discounts) {
                    $discounts .= " - ";
                }
                $discounts .= (string) $discount->code . ":" . (string) $discount->amount;
                $discount_total += (double) $discount->amount;
            }
        }
        $tax_total = 0;
        if (!empty($transaction->taxes)) {
            foreach ($transaction->taxes->tax as $tax) {
                $tax_total += (double) $tax->tax_amount;
            }
        }
        $product_total = 0;
        $future_product_total = 0;
        foreach ($transaction->transaction_details->transaction_detail as $transaction_detail) {
            $product_price = (double) $transaction_detail->product_price;
            foreach ($transaction_detail->transaction_detail_options->transaction_detail_option as $transaction_detail_option) {
                $product_price += (double) $transaction_detail_option->price_mod;
            }
            $product_total += $product_price * (int) $transaction_detail->product_quantity;
            if ($transaction_detail->is_future_line_item) {
                $future_product_total += $product_price * (int) $transaction_detail->product_quantity;
            }
        }
        $shipping_total = (double) $transaction->shipping_total;
        $order_total = $product_total + $shipping_total + $discount_total + $tax_total - $future_product_total;
        //Start Writing
        echo (string) $transaction->id;
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->store_id) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->transaction_date) . '"';
        echo $field_delimiter . $product_total;
        echo $field_delimiter . $tax_total;
        echo $field_delimiter . $shipping_total;
        echo $field_delimiter . $discount_total;
        echo $field_delimiter . $order_total;
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->purchase_order) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->cc_type) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->cc_number_masked) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->cc_exp_month) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->cc_exp_year) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->processor_response) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_id) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->is_anonymous) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->minfraud_score) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_first_name) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_last_name) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_company) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_address1) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_address2) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_city) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_state) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_postal_code) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_country) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_phone) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_email) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->customer_ip) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes($custom_fields) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes($attributes1) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes($discounts) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipto_shipping_service_description) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_first_name) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_last_name) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_company) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_address1) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_address2) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_city) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_state) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_postal_code) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_country) . '"';
        echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction->shipping_phone) . '"';
        //Products
        $product_count = 1;
        foreach ($transaction->transaction_details->transaction_detail as $transaction_detail) {
            //New Line for Second Product
            if ($product_count > 1) {
                echo (string) $transaction->id;
                for ($i = 1; $i <= 42; $i++) {
                    echo $field_delimiter . '""';
                }
            }
            //Options
            $product_options = "";
            foreach ($transaction_detail->transaction_detail_options->transaction_detail_option as $transaction_detail_option) {
                if ($product_options) {
                    $product_options .= " - ";
                }
                $product_options .= (string) $transaction_detail_option->product_option_name . ":" . (string) $transaction_detail_option->product_option_value;
            }
            echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction_detail->sub_token_url) . '"';
            echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction_detail->category_code) . '"';
            echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction_detail->product_name) . '"';
            echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction_detail->product_code) . '"';
            echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction_detail->product_price) . '"';
            echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction_detail->product_quantity) . '"';
            echo $field_delimiter . '"' . foxyshop_dblquotes((string) $transaction_detail->product_weight) . '"';
            echo $field_delimiter . '"' . foxyshop_dblquotes($product_options) . '"';
            echo "\n";
            $product_count++;
        }
    }
    die;
}