/**
 * wpsc_start_the_query
 */
function wpsc_start_the_query()
{
    if (is_404()) {
        return;
    }
    global $wpsc_page_titles, $wp_query, $wpsc_query, $wpsc_query_vars;
    $is_404 = $wp_query->is_404;
    if (null == $wpsc_query) {
        if ($wp_query->is_404 && !empty($wp_query->query_vars['paged']) || isset($wp_query->query['pagename']) && strpos($wp_query->query['pagename'], $wpsc_page_titles['products']) !== false && !isset($wp_query->post)) {
            global $post;
            $is_404 = true;
            if (!isset($wp_query->query_vars['wpsc_product_category']) && !isset($wp_query->query_vars['product_tag'])) {
                $wp_query = new WP_Query('post_type=wpsc-product&name=' . $wp_query->query_vars['name']);
            }
            if (isset($wp_query->post) && is_object($wp_query->post) && isset($wp_query->post->ID)) {
                $post = $wp_query->post;
            } else {
                $wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['name'];
            }
        }
        if (count($wpsc_query_vars) <= 1) {
            $wpsc_query_vars = array('post_status' => apply_filters('wpsc_product_display_status', array('publish')), 'post_parent' => 0, 'order' => apply_filters('wpsc_product_order', get_option('wpsc_product_order', 'ASC')), 'post_type' => apply_filters('wpsc_product_post_type', array('wpsc-product')));
            if ($wp_query->query_vars['preview']) {
                $wpsc_query_vars['post_status'] = 'any';
            }
            if (isset($_GET['product_order'])) {
                $wpsc_query_vars['order'] = $_GET['product_order'];
            }
            if (isset($wp_query->query_vars['product_tag'])) {
                $wpsc_query_vars['product_tag'] = $wp_query->query_vars['product_tag'];
                $wpsc_query_vars['taxonomy'] = get_query_var('taxonomy');
                $wpsc_query_vars['term'] = get_query_var('term');
            } elseif (isset($wp_query->query_vars['wpsc_product_category'])) {
                $wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['wpsc_product_category'];
                $wpsc_query_vars['taxonomy'] = get_query_var('taxonomy');
                $wpsc_query_vars['term'] = get_query_var('term');
            } else {
                $wpsc_query_vars['post_type'] = 'wpsc-product';
                $wpsc_query_vars['pagename'] = wpsc_get_page_slug('[productspage]');
            }
            if (1 == get_option('use_pagination')) {
                $wpsc_query_vars['nopaging'] = false;
                $wpsc_query_vars['posts_per_page'] = get_option('wpsc_products_per_page');
                $wpsc_query_vars['paged'] = get_query_var('paged');
                if (isset($wpsc_query_vars['paged']) && empty($wpsc_query_vars['paged'])) {
                    $wpsc_query_vars['paged'] = get_query_var('page');
                }
            }
            $orderby = isset($_GET['product_order']) ? 'title' : null;
            $wpsc_query_vars = array_merge($wpsc_query_vars, wpsc_product_sort_order_query_vars($orderby));
            add_filter('pre_get_posts', 'wpsc_generate_product_query', 11);
            $wpsc_query = new WP_Query($wpsc_query_vars);
            //for 3.1 :|
            if (empty($wpsc_query->posts) && isset($wpsc_query->tax_query) && isset($wp_query->query_vars['wpsc_product_category'])) {
                $wpsc_query_vars = array();
                $wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['wpsc_product_category'];
                if (1 == get_option('use_pagination')) {
                    $wpsc_query_vars['posts_per_page'] = get_option('wpsc_products_per_page');
                    $wpsc_query_vars['paged'] = get_query_var('paged');
                    if (empty($wpsc_query_vars['paged'])) {
                        $wpsc_query_vars['paged'] = get_query_var('page');
                    }
                }
                $wpsc_query = new WP_Query($wpsc_query_vars);
            }
        }
    }
    if ($is_404 || isset($wpsc_query->post_count) && $wpsc_query->post_count == 0 && isset($wpsc_query_vars['wpsc_product_category'])) {
        $args = array_merge($wp_query->query, array('posts_per_page' => get_option('wpsc_products_per_page')));
        $wp_query = new WP_Query($args);
        if (empty($wp_query->posts)) {
            $product_page_id = wpsc_get_the_post_id_by_shortcode('[productspage]');
            $wp_query = new WP_Query('page_id=' . $product_page_id);
        }
    }
    if (isset($wp_query->post) && is_object($wp_query->post) && isset($wp_query->post->ID)) {
        $post_id = $wp_query->post->ID;
    } else {
        $post_id = 0;
    }
    if (get_permalink($post_id) == get_option('shopping_cart_url')) {
        $_SESSION['wpsc_has_been_to_checkout'] = true;
    }
}
/**
 * wpsc display products function
 * @return string - html displaying one or more products
 */
function wpsc_display_products_page($query)
{
    global $wpdb, $wpsc_query, $wp_query, $wp_the_query;
    remove_filter('the_title', 'wpsc_the_category_title');
    // If the data is coming from a shortcode parse the values into the args variable,
    // I did it this was to preserve backwards compatibility
    if (!empty($query)) {
        $args = array();
        $args['post_type'] = 'wpsc-product';
        if (!empty($query['product_id']) && is_array($query['product_id'])) {
            $args['post__in'] = $query['product_id'];
        } elseif (is_string($query['product_id'])) {
            $args['post__in'] = (array) $query['product_id'];
        }
        if (!empty($query['old_product_id'])) {
            $post_id = wpsc_get_the_new_id($query['old_product_id']);
            $args['post__in'] = (array) $post_id;
        }
        if (!empty($query['price']) && 'sale' != $query['price']) {
            $args['meta_key'] = '_wpsc_price';
            $args['meta_value'] = $query['price'];
        } elseif (!empty($query['price']) && 'sale' == $query['price']) {
            $args['meta_key'] = '_wpsc_special_price';
            $args['meta_compare'] = '>=';
            $args['meta_value'] = '1';
        }
        if (!empty($query['product_name'])) {
            $args['pagename'] = $query['product_name'];
        }
        if (!empty($query['category_id'])) {
            $term = get_term($query['category_id'], 'wpsc_product_category');
            $id = wpsc_get_meta($query['category_id'], 'category_id', 'wpsc_old_category');
            if (!empty($id)) {
                $term = get_term($id, 'wpsc_product_category');
                $args['wpsc_product_category'] = $term->slug;
                $args['wpsc_product_category__in'] = $term->term_id;
            } else {
                $args['wpsc_product_category'] = $term->slug;
                $args['wpsc_product_category__in'] = $term->term_id;
            }
        }
        if (!empty($query['category_url_name'])) {
            $args['wpsc_product_category'] = $query['category_url_name'];
        }
        $orderby = !empty($query['sort_order']) ? $query['sort_order'] : null;
        $args = array_merge($args, wpsc_product_sort_order_query_vars($orderby));
        if (!empty($query['order'])) {
            $args['order'] = $query['order'];
        }
        if (!empty($query['limit_of_items']) && '1' == get_option('use_pagination')) {
            $args['posts_per_page'] = $query['limit_of_items'];
        }
        if (!empty($query['number_per_page']) && '1' == get_option('use_pagination')) {
            $args['posts_per_page'] = $query['number_per_page'];
            $args['paged'] = $query['page'];
        }
        if ('0' == get_option('use_pagination')) {
            $args['nopaging'] = true;
            $args['posts_per_page'] = '-1';
        }
        if (!empty($query['tag'])) {
            $args['product_tag'] = $query['tag'];
        }
        query_posts($args);
    }
    // swap the wpsc_query objects
    $GLOBALS['nzshpcrt_activateshpcrt'] = true;
    // Pretty sure this single_product code is legacy...but fixing it up just in case.
    // get the display type for the selected category
    if (!empty($wpsc_query->query_vars['term'])) {
        $display_type = wpsc_get_the_category_display($wpsc_query->query_vars['term']);
    } elseif (!empty($args['wpsc_product_category'])) {
        $display_type = wpsc_get_the_category_display($args['wpsc_product_category']);
    } else {
        $display_type = 'default';
    }
    $saved_display = wpsc_get_customer_meta('display_type');
    $display_type = !empty($saved_display) ? $saved_display : $display_type;
    ob_start();
    do_action('wpsc_display_products_page', $display_type);
    if (isset($wp_query->post) && 'wpsc-product' == $wp_query->post->post_type && !is_archive() && $wp_query->post_count <= 1) {
        include wpsc_get_template_file_path('wpsc-single_product.php');
    } else {
        wpsc_include_products_page_template($display_type);
    }
    $output = ob_get_contents();
    ob_end_clean();
    $output = str_replace('\\$', '$', $output);
    if (!empty($query)) {
        wp_reset_query();
        wp_reset_postdata();
    }
    return $output;
}