Beispiel #1
0
 function &get_products()
 {
     global $wpdb, $wp_query;
     do_action_ref_array('pre_get_products', array(&$this));
     if ($this->query_vars['category_url_name'] != '') {
         $category_data = $wpdb->get_row("SELECT `id`, `image_height`, `image_width` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `active`='1' AND `nice-name` = '{$this->query_vars['category_url_name']}' LIMIT 1", ARRAY_A);
         $this->query_vars['category_id'] = $category_data['id'];
         $this->category = $this->query_vars['category_id'];
     } else {
         if ($this->query_vars['category_id'] > 0) {
             $category_data = $wpdb->get_row("SELECT `image_height`, `image_width` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `active`='1' AND `id` = '{$this->query_vars['category_id']}' LIMIT 1", ARRAY_A);
         }
     }
     if (is_array($category_data)) {
         $this->category_product['image_height'] = $category_data['image_height'];
         $this->category_product['image_width'] = $category_data['image_width'];
     }
     if ($this->query_vars['product_url_name'] != null) {
         $product_id = $wpdb->get_var("SELECT `product_id` FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `meta_key` IN ( 'url_name' ) AND `meta_value` IN ( '" . stripslashes($this->query_vars['product_url_name']) . "' ) ORDER BY `product_id` DESC LIMIT 1");
         //echo "SELECT `product_id` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN ( 'url_name' ) AND `meta_value` IN ( '".stripslashes($this->query_vars['product_url_name'])."' ) ORDER BY `product_id` DESC LIMIT 1<br />";
     } else {
         $product_id = absint($this->query_vars['product_id']);
     }
     if ($product_id > 0) {
         $product_list = $wpdb->get_results("SELECT * FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id`='" . (int) $product_id . "' AND `active` IN('1') AND `publish` IN('1') LIMIT 1", ARRAY_A);
     }
     if (isset($_SESSION['price_range']) && isset($_GET['range'])) {
         if (is_numeric($_GET['range']) || isset($_SESSION['price_range'])) {
             $price_ranges = $_SESSION['price_range'];
             $selected_price_point = absint($_GET['range']);
             $next_price_point = $selected_price_point + 1;
             //echo "<pre>".print_r($ranges,true)."</pre>";
             $product_range_sql_parts = array();
             $variation_sql_parts = array();
             $product_sql_parts = array();
             if (isset($price_ranges[$selected_price_point])) {
                 $product_range_sql_parts[] = "(`price` - `special_price`) >= '" . absint($price_ranges[$selected_price_point]) . "'";
                 $variation_sql_parts[] = "`price` >= '" . absint($price_ranges[$selected_price_point]) . "'";
                 if (isset($price_ranges[$next_price_point])) {
                     $product_range_sql_parts[] = "(`price` - `special_price`) < '" . absint($price_ranges[$next_price_point]) . "'";
                     $variation_sql_parts[] = "`price` < '" . absint($price_ranges[$next_price_point]) . "'";
                 }
                 $variation_product_ids = (array) $wpdb->get_col("SELECT DISTINCT `product_id` FROM `" . WPSC_TABLE_VARIATION_PROPERTIES . "` WHERE " . implode(" AND ", $variation_sql_parts) . "");
                 if (count($variation_product_ids) > 0) {
                     $product_sql_parts[] = "( (" . implode(" AND ", $product_range_sql_parts) . ") OR `id` IN('" . implode("', '", $variation_product_ids) . "') )";
                 } else {
                     $product_sql_parts += $product_range_sql_parts;
                 }
             }
             $product_sql_parts[] = "`active` IN ('1')";
             $product_sql_parts[] = "`publish` IN('1')";
             $range_sql = "SELECT * FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE " . implode(" AND ", $product_sql_parts) . "";
             //echo $range_sql;
             $product_list = $wpdb->get_results($range_sql, ARRAY_A);
         }
     }
     //exit('Here:<pre>'.print_r($this->query_vars, true).'</pre>');
     if (count($product_list) > 0 && !isset($_GET['range'])) {
         // if is a single product
         $this->is_single = true;
         $this->products = $product_list;
         $this->category = $this->query_vars['category_id'];
     } elseif (count($product_list) > 0 && isset($_GET['range'])) {
         $this->is_single = false;
         $this->products = $product_list;
         $this->category = $this->query_vars['category_id'];
     } else {
         // Otherwise
         //if we are using pages, how many items per page and what page?
         if (get_option('use_pagination') == 1) {
             $products_per_page = $this->query_vars['number_per_page'];
             if ($this->query_vars['page'] > 0) {
                 $startnum = ($this->query_vars['page'] - 1) * $products_per_page;
             } else {
                 $startnum = 0;
             }
         } else {
             $startnum = 0;
         }
         // search section is done here
         if (function_exists('gold_shpcrt_search_sql') && $_GET['product_search'] != '') {
             $search_sql = gold_shpcrt_search_sql();
             if ($search_sql != '') {
                 // this cannot currently list products that are associated with no categories
                 $rowcount = $wpdb->get_var("SELECT COUNT( DISTINCT `" . WPSC_TABLE_PRODUCT_LIST . "`.`id`) AS `count` FROM `" . WPSC_TABLE_PRODUCT_LIST . "`,`" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` WHERE `" . WPSC_TABLE_PRODUCT_LIST . "`.`publish`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`active`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`id` = `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "`.`product_id` AND `" . WPSC_TABLE_PRODUCT_LIST . "`.donation = '0' {$search_sql}");
                 if (isset($_SESSION['item_per_page'])) {
                     $products_per_page = $_SESSION['item_per_page'];
                 }
                 //exit($products_per_page);
                 if (!is_numeric($products_per_page) || $products_per_page < 1) {
                     $products_per_page = $rowcount;
                 }
                 if ($startnum >= $rowcount) {
                     $startnum = 0;
                 }
                 $sql = "SELECT DISTINCT `" . WPSC_TABLE_PRODUCT_LIST . "`.* FROM `" . WPSC_TABLE_PRODUCT_LIST . "`,`" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` WHERE `" . WPSC_TABLE_PRODUCT_LIST . "`.`publish`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`active`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`id` = `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "`.`product_id` {$no_donations_sql} {$search_sql} ORDER BY `" . WPSC_TABLE_PRODUCT_LIST . "`.`special` DESC LIMIT {$startnum}, {$products_per_page}";
             }
         } else {
             if ($wp_query->query_vars['ptag'] != null || $_GET['ptag'] != null) {
                 // search by tags is done here
                 if ($wp_query->query_vars['ptag'] != null) {
                     $tag = $wp_query->query_vars['ptag'];
                 } else {
                     $tag = $_GET['ptag'];
                 }
                 $results = $wpdb->get_results("SELECT * FROM `{$wpdb->terms}` WHERE slug='{$tag}'");
                 $term_id = $results[0]->term_id;
                 $results = $wpdb->get_results("SELECT * FROM `{$wpdb->term_taxonomy}` WHERE term_id = '" . $term_id . "' AND taxonomy='product_tag'");
                 $taxonomy_id = $results[0]->term_taxonomy_id;
                 $results = $wpdb->get_results("SELECT * FROM `{$wpdb->term_relationships}` WHERE term_taxonomy_id = '" . $taxonomy_id . "'");
                 foreach ($results as $result) {
                     $product_ids[] = $result->object_id;
                 }
                 $product_id = implode(",", $product_ids);
                 $sql = "SELECT * FROM " . WPSC_TABLE_PRODUCT_LIST . " WHERE id IN (" . $product_id . ") AND `publish` IN('1') AND `active` IN('1')";
                 //Transom - added publish & active
             } else {
                 // select by category is done here
                 if (is_numeric($this->query_vars['category_id']) && $this->query_vars['category_id'] > 0) {
                     /*
                      * The reason this is so complicated is because of the product ordering, it is done by category/product association
                      * If you can see a way of simplifying it and speeding it up, then go for it.
                      */
                     $rowcount = $wpdb->get_var("SELECT COUNT( DISTINCT `" . WPSC_TABLE_PRODUCT_LIST . "`.`id`) AS `count` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` LEFT JOIN `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` ON `" . WPSC_TABLE_PRODUCT_LIST . "`.`id` = `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "`.`product_id` WHERE `" . WPSC_TABLE_PRODUCT_LIST . "`.`publish`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`active` = '1' AND `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "`.`category_id` IN ('" . $this->query_vars['category_id'] . "') {$no_donations_sql}");
                     if (!is_numeric($products_per_page) || $products_per_page < 1) {
                         $products_per_page = $rowcount;
                     }
                     if ($startnum >= $rowcount) {
                         $startnum = 0;
                     }
                     if ($this->query_vars['sort_order'] == 'DESC') {
                         $order = 'DESC';
                     } else {
                         $order = 'ASC';
                     }
                     // Invert this for alphabetical ordering.
                     if (get_option('wpsc_sort_by') == 'name') {
                         if ($order == 'ASC') {
                             $order = 'DESC';
                         } else {
                             $order = 'ASC';
                         }
                         $order_by = "`products`.`name` {$order}";
                     } else {
                         if (get_option('wpsc_sort_by') == 'price') {
                             $order_by = "`products`.`price` {$order}";
                         } else {
                             //$order = 'ASC';
                             if ($order == 'ASC') {
                                 $product_id_order = 'DESC';
                             } else {
                                 $product_id_order = 'ASC';
                             }
                             $order_by = " `order_state` DESC,`order`.`order` {$order}, `products`.`id` {$product_id_order}";
                             //$order_by = " `order_state` DESC, `products`.`id` $product_id_order,`order`.`order` $order";
                         }
                     }
                     $sql = "SELECT DISTINCT `products`.*, `category`.`category_id`,`order`.`order`, IF(ISNULL(`order`.`order`), 0, 1) AS `order_state` \n\t\t\t\tFROM `" . WPSC_TABLE_PRODUCT_LIST . "` AS `products`\n\t\t\t\tLEFT JOIN `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` AS `category`\n\t\t\t\t\t ON `products`.`id` = `category`.`product_id` \n\t\t\t\tLEFT JOIN `" . WPSC_TABLE_PRODUCT_ORDER . "`\tAS `order`\n\t\t\t\t\tON (\n\t\t\t\t\t\t ( `products`.`id` = `order`.`product_id` )\n\t\t\t\t\t\t AND\n\t\t\t\t\t\t ( `category`.`category_id` = `order`.`category_id` ) \n\t\t\t\t\t) \n\t\t\t\tWHERE `products`.`publish`='1'\n\t\t\t\tAND `products`.`active` = '1'\n\t\t\t\tAND `category`.`category_id` IN ('" . (int) $this->query_vars['category_id'] . "') {$no_donations_sql}\n\t\t\t\tORDER BY {$order_by} LIMIT {$startnum}, {$products_per_page}";
                 } else {
                     if ($this->query_vars['sort_order'] == 'DESC') {
                         $order = 'DESC';
                     } else {
                         $order = 'ASC';
                     }
                     if (get_option('wpsc_sort_by') == 'name') {
                         $order_by = "`" . WPSC_TABLE_PRODUCT_LIST . "`.`name` {$order}";
                     } else {
                         if (get_option('wpsc_sort_by') == 'price') {
                             $order_by = "`" . WPSC_TABLE_PRODUCT_LIST . "`.`price` {$order}";
                         } else {
                             if ($order == 'ASC') {
                                 $order = 'DESC';
                             } else {
                                 $order = 'ASC';
                             }
                             $order_by = "`" . WPSC_TABLE_PRODUCT_LIST . "`.`id` {$order}";
                         }
                     }
                     $rowcount = $wpdb->get_var("SELECT COUNT( DISTINCT `" . WPSC_TABLE_PRODUCT_LIST . "`.`id`) AS `count` FROM `" . WPSC_TABLE_PRODUCT_LIST . "`,`" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` WHERE `" . WPSC_TABLE_PRODUCT_LIST . "`.`publish`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`active`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`id` = `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "`.`product_id` {$no_donations_sql} {$group_sql}");
                     if (!is_numeric($products_per_page) || $products_per_page < 1) {
                         $products_per_page = $rowcount;
                     }
                     if ($startnum >= $rowcount) {
                         $startnum = 0;
                     }
                     $sql = "SELECT DISTINCT `" . WPSC_TABLE_PRODUCT_LIST . "`.* FROM `" . WPSC_TABLE_PRODUCT_LIST . "`,`" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` WHERE `" . WPSC_TABLE_PRODUCT_LIST . "`.`publish`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`active`='1' AND `" . WPSC_TABLE_PRODUCT_LIST . "`.`id` = `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "`.`product_id` {$no_donations_sql} {$group_sql} ORDER BY `" . WPSC_TABLE_PRODUCT_LIST . "`.`special`, {$order_by} LIMIT {$startnum}, {$products_per_page}";
                 }
             }
         }
         //echo "{$sql}";
         $this->category = $this->query_vars['category_id'];
         $this->products = $wpdb->get_results($sql, ARRAY_A);
         if ($rowcount > $products_per_page) {
             if ($products_per_page > 0) {
                 $pages = ceil($rowcount / $products_per_page);
             } else {
                 $pages = 1;
             }
         }
     }
     if (get_option('permalink_structure') != '') {
         $seperator = "?";
     } else {
         $seperator = "&amp;";
     }
     $product_view_url = wpsc_category_url($this->category);
     if (!is_numeric($_GET['category']) && $_GET['product_search'] != '') {
         $product_view_url = add_query_arg('product_search', $_GET['product_search'], $product_view_url);
         //$product_view_url_not_used .= "product_search=".$_GET['product_search']."&amp;"."view_type=".$_GET['view_type']."&amp;"."item_per_page=".$_GET['item_per_page']."&amp;";
     }
     if (!is_numeric($_GET['category']) && $_GET['item_per_page'] > 0) {
         $product_view_url = add_query_arg('item_per_page', $_GET['item_per_page'], $product_view_url);
     }
     if (isset($_GET['order']) && $_GET['order'] == 'ASC' || $_GET['order'] == 'DESC') {
         $product_view_url = add_query_arg('order', $_GET['order'], $product_view_url);
     }
     if (isset($_GET['view_type']) && $_GET['view_type'] == 'default' || $_GET['view_type'] == 'grid') {
         $product_view_url = add_query_arg('view_type', $_GET['view_type'], $product_view_url);
     }
     for ($i = 1; $i <= $pages; $i++) {
         if ($this->query_vars['page'] == $i || $this->query_vars['page'] <= $i && $i <= 1) {
             if ($_GET['view_all'] != 'true') {
                 $selected = true;
             }
         } else {
             $selected = false;
         }
         if (get_option('permalink_structure')) {
             //if()
             $page_url = wpsc_category_url($this->category, true) . "page/{$i}/";
         } else {
             $page_url = add_query_arg('page_number', $i, $product_view_url);
         }
         $this->pages[$i - 1]['number'] = $i;
         $this->pages[$i - 1]['url'] = $page_url;
         $this->pages[$i - 1]['selected'] = $selected;
     }
     $this->page_count = count($this->pages);
     //if ( !$q['suppress_filters'] )
     $this->products = apply_filters('the_products', $this->products);
     $this->product_count = count($this->products);
     if ($this->product_count > 0) {
         $this->product = $this->products[0];
     }
     // get the breadcrumbs
     $this->get_breadcrumbs();
     return $this->products;
 }
Beispiel #2
0
    function wpsc_gold_shpcrt_ajax($id)
    {
        global $wpdb;
        if (isset($_POST) && !empty($_POST)) {
            if (isset($_POST['wpsc_live_search']) && $_POST['wpsc_live_search'] == true && (get_option('show_live_search') == 1 || true == $_POST['wpsc_search_widget']) && !empty($_POST['product_search'])) {
                $keyword = $_POST['product_search'];
                $output = "<ul>";
                if (version_compare(WPSC_VERSION, '3.8', '<')) {
                    $search_sql = gold_shpcrt_search_sql($keyword);
                    $product_list = $wpdb->get_results("SELECT DISTINCT `" . WPSC_TABLE_PRODUCT_LIST . "`.* FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `" . WPSC_TABLE_PRODUCT_LIST . "`.`active`='1' {$search_sql} ORDER BY `" . WPSC_TABLE_PRODUCT_LIST . "`.`name` ASC", ARRAY_A);
                    if ($product_list != null) {
                        foreach ($product_list as $product) {
                            //filter out the HTML, otherwise we get partial tags and everything breaks
                            $product['description'] = wp_kses($product['description'], false);
                            // shorten the description;
                            if (strlen($product['description']) > 68) {
                                $product_description = substr($product['description'], 0, 68) . "...";
                            } else {
                                $product_description = $product['description'];
                            }
                            //generate the HTML
                            $output .= "<li>\n\r";
                            $output .= "\t<a href='" . wpsc_product_url($product['id']) . "'>\n\r";
                            if ($product['image'] != '') {
                                $output .= "\t\t\t\t<img class='live-search-image' src='index.php?productid=" . $product['id'] . "&amp;width=50&amp;height=50'>\n\r";
                            } else {
                                $output .= "\t\t\t\t<img class='live-search-image' src='" . get_option('siteurl') . "/wp-content/plugins/" . WPSC_DIR_NAME . "/no-image-uploaded.gif' style='height: 50px; width: 50px;'>\n\r";
                            }
                            $output .= "\t\t\t\t<div class='live-search-text'>\n\r";
                            $output .= "\t\t\t\t\t<strong>" . $product['name'] . "</strong>\n\r";
                            $output .= "\t\t\t\t\t<div class='description'>" . stripslashes($product_description) . "</div>\n\r";
                            $output .= "\t\t\t\t</div>\n\r";
                            $output .= "\t\t    <br clear='both' />\n\r";
                            $output .= "\t\t</a>\n\r";
                            $output .= "</li>\n\r";
                        }
                    }
                } else {
                    wpsc_gc_start_search_query();
                    echo '<ul>';
                    while (wpsc_have_products()) {
                        wpsc_the_product();
                        ?>
					<li>
						<a style="clear:both;" href="<?php 
                        echo wpsc_the_product_permalink();
                        ?>
">
							<?php 
                        if (wpsc_the_product_thumbnail()) {
                            ?>
								<img class="live-search-image" alt="<?php 
                            echo wpsc_the_product_title();
                            ?>
" src="<?php 
                            echo wpsc_the_product_thumbnail(50, 50, 0, 'live-search');
                            ?>
" />
							<?php 
                        } else {
                            ?>
								<img class="live-search-image" alt="No Image" title="<?php 
                            echo wpsc_the_product_title();
                            ?>
" src="<?php 
                            echo WPSC_CORE_THEME_URL;
                            ?>
wpsc-images/noimage.png" style="width:50px; height:50px;" />
							<?php 
                        }
                        ?>
							<div class="live-search-text">
								<strong><?php 
                        echo wpsc_the_product_title();
                        ?>
</strong>
								<div class="description">
									<?php 
                        echo wpsc_the_product_description();
                        ?>
								</div>
							</div>
						</a>
					</li>	
					<?php 
                    }
                    echo '</ul>';
                    exit;
                }
                $output .= "</ul>";
                if (!empty($product_list)) {
                    $_SESSION['live_search_results'] = $product_list;
                }
                exit($output);
            }
        }
        if (isset($_POST['affiliate']) && $_POST['affiliate'] == true) {
            if (!function_exists('affiliate_text')) {
                function affiliate_text($id, $user)
                {
                    $output = "<a target='_blank' title='Your Shopping Cart' href='" . get_option('siteurl') . "/?action=affiliate&p={$id}&user_id=" . $user . "&height=400&width=600' class='thickbox'><img src='" . WPSC_URL . "/" . $images_dir . "/buynow.jpg'></a>";
                    return $output;
                }
            }
            $id = $_POST['prodid'];
            $product = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE id='{$id}' LIMIT 1", ARRAY_A);
            $product = $product[0];
            $link = affiliate_text($id, $_POST['uid']);
            echo "<textarea class='affiliate_text' onclick='this.select();' >{$link}</textarea>";
            exit;
        }
        if (isset($_POST['log_affiliate']) && $_POST['log_affiliate'] == true) {
            $uid = $_POST['uid'];
            $amount = $_POST['amount'];
            $product = $wpdb->query("UPDATE {$wpdb->prefix}wpsc_affiliates SET paid=paid+{$amount}  WHERE user_id='{$uid}'");
            echo "uid=" . $uid;
            exit;
        }
    }
function wpsc_get_product_listing($product_list, $group_type, $group_sql = '', $search_sql = '')
{
    global $wpdb, $wp_query;
    if (WPSC_DEBUG === true) {
        wpsc_debug_start_subtimer('wpsc_get_product_listing', 'start');
    }
    $siteurl = get_option('siteurl');
    $activated_widgets = get_option('sidebars_widgets');
    //     echo("<pre>".print_r($wp_query->query_vars,true)."</pre>");
    if (get_option('permalink_structure') != '') {
        $seperator = "?";
    } else {
        $seperator = "&amp;";
    }
    if (isset($_GET['action']) && $_GET['action'] == 'bfg' && isset($_GET['session']) && $_GET['session'] == $_SESSION['google_session']) {
        $_SESSION['nzshpcrt_cart'] = '';
        $_SESSION['nzshpcrt_cart'] = array();
        unset($_SESSION['coupon_num'], $_SESSION['google_session']);
    }
    if (isset($_GET['items_per_page']) && $_GET['items_per_page'] != 0) {
        update_option('use_pagination', 1);
    }
    if (get_option('use_pagination') == 1) {
        $products_per_page = get_option('wpsc_products_per_page');
        if (isset($_REQUEST['items_per_page'])) {
            $products_per_page = $_REQUEST['items_per_page'];
        }
        if ($_GET['page_number'] > 0) {
            $startnum = ($_GET['page_number'] - 1) * $products_per_page;
        } else {
            $startnum = 0;
        }
    } else {
        $startnum = 0;
    }
    if (is_numeric($wp_query->query_vars['product_category'])) {
        $category_id = $wp_query->query_vars['product_category'];
    } else {
        if (is_numeric($_GET['category'])) {
            $category_id = $_GET['category'];
        } else {
            if (is_numeric($GLOBALS['wpsc_category_id'])) {
                $category_id = $GLOBALS['wpsc_category_id'];
            }
        }
    }
    //echo("<pre>".print_r($category_id,true)."</pre>");
    if (is_numeric($_GET['range'])) {
        $ranges = $_SESSION['price_range'];
        // 	    exit("<pre>".print_r($ranges,1)."</pre>");
        switch ($_GET['range']) {
            case 1:
                $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` < " . $ranges[1] . " AND `active` IN ('1')";
                break;
            case 2:
                if (array_key_exists(2, $ranges)) {
                    $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[1] . "' AND `price` < '" . $ranges[2] . "' AND `active` IN ('1')";
                } else {
                    $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[1] . "' AND `active` IN ('1')";
                }
                break;
            case 3:
                if (array_key_exists(3, $ranges)) {
                    $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[2] . "' AND `price` < '" . $ranges[3] . "' AND `active` IN ('1')";
                } else {
                    $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[2] . "' AND `active` IN ('1')";
                }
                break;
            case 4:
                if (array_key_exists(4, $ranges)) {
                    $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[3] . "' AND `price` < '" . $ranges[4] . "' AND `active` IN ('1')";
                } else {
                    $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[3] . "' AND `active` IN ('1')";
                }
                break;
            case 5:
                if (array_key_exists(5, $ranges)) {
                    $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[4] . "' AND `price` < '" . $ranges[5] . "' AND `active` IN ('1')";
                } else {
                    $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[4] . "' AND `active` IN ('1')";
                }
                break;
            case 6:
                $range_sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE `price` >= '" . $ranges[5] . "' AND `active` IN ('1')";
                break;
        }
        //exit($range_sql);
        $product_list = $wpdb->get_results($range_sql, ARRAY_A);
        return array("product_list" => $product_list, "page_listing" => '');
    }
    foreach ((array) $activated_widgets as $widget_container) {
        if (is_array($widget_container) && array_search(TXT_WPSC_DONATIONS, $widget_container)) {
            $no_donations_sql = "AND `" . $wpdb->prefix . "product_list`.`donation` != '1'";
            break;
        }
    }
    if (function_exists('gold_shpcrt_search_sql') && $_GET['product_search'] != '') {
        $search_sql = gold_shpcrt_search_sql();
        if ($search_sql != '') {
            // this cannot currently list products that are associated with no categories
            $rowcount = $wpdb->get_var("SELECT DISTINCT COUNT(`" . $wpdb->prefix . "product_list`.`id`) AS `count` FROM `" . $wpdb->prefix . "product_list`,`" . $wpdb->prefix . "item_category_associations` WHERE `" . $wpdb->prefix . "product_list`.`active`='1' AND `" . $wpdb->prefix . "product_list`.`id` = `" . $wpdb->prefix . "item_category_associations`.`product_id` {$no_donations_sql} {$search_sql}");
            if (isset($_SESSION['item_per_page'])) {
                $products_per_page = $_SESSION['item_per_page'];
            }
            //exit($products_per_page);
            if (!is_numeric($products_per_page) || $products_per_page < 1) {
                $products_per_page = $rowcount;
            }
            if ($startnum >= $rowcount && $rowcount - $products_per_page >= 0) {
                $startnum = $rowcount - $products_per_page;
            }
            $sql = "SELECT DISTINCT `" . $wpdb->prefix . "product_list`.* FROM `" . $wpdb->prefix . "product_list`,`" . $wpdb->prefix . "item_category_associations` WHERE `" . $wpdb->prefix . "product_list`.`active`='1' AND `" . $wpdb->prefix . "product_list`.`id` = `" . $wpdb->prefix . "item_category_associations`.`product_id` {$no_donations_sql} {$search_sql} ORDER BY `" . $wpdb->prefix . "product_list`.`special` DESC LIMIT {$startnum}, {$products_per_page}";
        }
    } else {
        if ($wp_query->query_vars['ptag'] != null || $_GET['ptag'] != null) {
            if ($wp_query->query_vars['ptag'] != null) {
                $tag = $wp_query->query_vars['ptag'];
            } else {
                $tag = $_GET['ptag'];
            }
            $results = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "terms WHERE slug='{$tag}'");
            $term_id = $results[0]->term_id;
            $results = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "term_taxonomy WHERE term_id = '" . $term_id . "' AND taxonomy='product_tag'");
            $taxonomy_id = $results[0]->term_taxonomy_id;
            $results = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "term_relationships WHERE term_taxonomy_id = '" . $taxonomy_id . "'");
            foreach ($results as $result) {
                $product_ids[] = $result->object_id;
            }
            $product_id = implode(",", $product_ids);
            $sql = "SELECT * FROM " . $wpdb->prefix . "product_list WHERE id IN (" . $product_id . ")";
        } else {
            if (is_numeric($_GET['category']) || is_numeric($wp_query->query_vars['product_category']) || is_numeric(get_option('wpsc_default_category'))) {
                if ($wp_query->query_vars['product_category'] != null) {
                    $catid = $wp_query->query_vars['product_category'];
                } else {
                    if (is_numeric($_GET['category'])) {
                        $catid = $_GET['category'];
                    } else {
                        if (is_numeric($GLOBALS['wpsc_category_id'])) {
                            $catid = $GLOBALS['wpsc_category_id'];
                        } else {
                            $catid = get_option('wpsc_default_category');
                        }
                    }
                }
                /*
                 * The reason this is so complicated is because of the product ordering, it is done by category/product association
                 * If you can see a way of simplifying it and speeding it up, then go for it.
                 */
                $rowcount = $wpdb->get_var("SELECT DISTINCT COUNT(`" . $wpdb->prefix . "product_list`.`id`) AS `count` FROM `" . $wpdb->prefix . "product_list` LEFT JOIN `" . $wpdb->prefix . "item_category_associations` ON `" . $wpdb->prefix . "product_list`.`id` = `" . $wpdb->prefix . "item_category_associations`.`product_id` WHERE `" . $wpdb->prefix . "product_list`.`active` = '1' AND `" . $wpdb->prefix . "item_category_associations`.`category_id` IN ('" . $catid . "') {$no_donations_sql}");
                if (!is_numeric($products_per_page) || $products_per_page < 1) {
                    $products_per_page = $rowcount;
                }
                if ($startnum >= $rowcount && $rowcount - $products_per_page >= 0) {
                    $startnum = $rowcount - $products_per_page;
                }
                if ($_REQUEST['order'] == null) {
                    $order = 'ASC';
                } elseif ($_REQUEST['order'] == 'DESC') {
                    $order = 'DESC';
                }
                $sql = "SELECT DISTINCT `" . $wpdb->prefix . "product_list`.*, `" . $wpdb->prefix . "item_category_associations`.`category_id`,`" . $wpdb->prefix . "product_order`.`order`, IF(ISNULL(`" . $wpdb->prefix . "product_order`.`order`), 0, 1) AS `order_state` FROM `" . $wpdb->prefix . "product_list` LEFT JOIN `" . $wpdb->prefix . "item_category_associations` ON `" . $wpdb->prefix . "product_list`.`id` = `" . $wpdb->prefix . "item_category_associations`.`product_id` LEFT JOIN `" . $wpdb->prefix . "product_order` ON ( ( `" . $wpdb->prefix . "product_list`.`id` = `" . $wpdb->prefix . "product_order`.`product_id` ) AND ( `" . $wpdb->prefix . "item_category_associations`.`category_id` = `" . $wpdb->prefix . "product_order`.`category_id` ) ) WHERE `" . $wpdb->prefix . "product_list`.`active` = '1' AND `" . $wpdb->prefix . "item_category_associations`.`category_id` IN ('" . $catid . "') {$no_donations_sql} ORDER BY `order_state` DESC,`" . $wpdb->prefix . "product_order`.`order` {$order}, `" . $wpdb->prefix . "product_list`.`id` DESC LIMIT {$startnum}, {$products_per_page}";
                //exit($sql);
            } else {
                $rowcount = $wpdb->get_var("SELECT DISTINCT COUNT(`" . $wpdb->prefix . "product_list`.`id`) AS `count` FROM `" . $wpdb->prefix . "product_list`,`" . $wpdb->prefix . "item_category_associations` WHERE `" . $wpdb->prefix . "product_list`.`active`='1' AND `" . $wpdb->prefix . "product_list`.`id` = `" . $wpdb->prefix . "item_category_associations`.`product_id` {$no_donations_sql} {$group_sql}");
                if (!is_numeric($products_per_page) || $products_per_page < 1) {
                    $products_per_page = $rowcount;
                }
                if ($startnum >= $rowcount && $rowcount - $products_per_page >= 0) {
                    $startnum = $rowcount - $products_per_page;
                }
                $sql = "SELECT DISTINCT `" . $wpdb->prefix . "product_list`.* FROM `" . $wpdb->prefix . "product_list`,`" . $wpdb->prefix . "item_category_associations` WHERE `" . $wpdb->prefix . "product_list`.`active`='1' AND `" . $wpdb->prefix . "product_list`.`id` = `" . $wpdb->prefix . "item_category_associations`.`product_id` {$no_donations_sql} {$group_sql} ORDER BY `" . $wpdb->prefix . "product_list`.`special`, `" . $wpdb->prefix . "product_list`.`id`  DESC LIMIT {$startnum}, {$products_per_page}";
            }
        }
    }
    // shows page numbers, probably fairly obviously
    // exit($sql);
    $return_array['product_list'] = $wpdb->get_results($sql, ARRAY_A);
    $return_array['page_listing'] = "";
    if ($rowcount > $products_per_page) {
        if ($products_per_page > 0) {
            $pages = ceil($rowcount / $products_per_page);
        } else {
            $pages = 1;
        }
        //$product_view_url = get_option('product_list_url').$seperator;
        $product_view_url = wpsc_category_url($category_id) . $seperator;
        if (is_numeric($_GET['category'])) {
        } else {
            if (is_numeric($_GET['brand'])) {
                $product_view_url .= "brand=" . $_GET['brand'] . "&amp;";
            } else {
                if ($_GET['product_search'] != '') {
                    $product_view_url .= "product_search=" . $_GET['product_search'] . "&amp;" . "view_type=" . $_GET['view_type'] . "&amp;" . "item_per_page=" . $_GET['item_per_page'] . "&amp;";
                }
            }
        }
        if (isset($_GET['order']) && $_GET['order'] == 'ASC' || $_GET['order'] == 'DESC') {
            $product_view_url .= "order={$_GET['order']}&amp;";
        }
        if (isset($_GET['view_type']) && $_GET['view_type'] == 'default' || $_GET['view_type'] == 'grid') {
            $product_view_url .= "view_type={$_GET['view_type']}&amp;";
        }
        $return_array['page_listing'] .= "<div class='wpsc_page_numbers'>\n\r";
        $return_array['page_listing'] .= "Pages: ";
        for ($i = 1; $i <= $pages; ++$i) {
            if ($_GET['page_number'] == $i || !is_numeric($_GET['page_number']) && $i == 0) {
                if ($_GET['view_all'] != 'true') {
                    $selected = "class='selected'";
                }
            } else {
                $selected = "class='notselected'";
            }
            $return_array['page_listing'] .= "  <a href='" . $product_view_url . "page_number={$i}' {$selected} >{$i}</a>\n\r";
        }
        $return_array['page_listing'] .= "</div>\n\r";
    }
    $return_array['category_id'] = $catid;
    if (WPSC_DEBUG === true) {
        wpsc_debug_start_subtimer('wpsc_get_product_listing', 'stop');
    }
    return $return_array;
}