Jump to content

Category Products - default sort order by `in stock`, then `popularity


disco_ii_disco

Recommended Posts

Hi all,

 

Just in the process of developing my new Cubecart 5 store.

 

In my old v4 store I order category products, showing those that are in stock first. The "in stock products" are then sorted by popularity.

 

I am attempting to do this in v5.

 

I have the following query in the getCategoryProducts function (clas/catalogue.class.php):

$newQuery = 'SELECT `I`.`product_id`,
         `I`.`stock_level` 
      FROM `CubeCart_inventory` AS `I`
        INNER JOIN `CubeCart_category_index` AS `C` 
            ON (`I`.`product_id` = `C`.`product_id`)
        INNER JOIN `CubeCart_category` AS `CAT`
            ON (`C`.`cat_id` = `CAT`.`cat_id`)
      WHERE  
       `I`.`status` =1 
    	 AND (`C`.`cat_id` ='.$category_id.'
    	 OR `CAT`.`cat_parent_id` ='.$category_id.')
      GROUP BY `I`.`product_id` ORDER BY `I`.`stock_level`=0,`I`.`popularity` DESC';

This gets the products sorted correctly - in mysql - but they are then being "resorted" by Cubecart.

 

I am struggling to find where to override CubeCart's sorting function to leave this order intact!

 

I have tried removing numerous lines from getProductData() and getCategoryProducts() but no joy so far.

 

Any help would be appreciated.

 

Regards,

Chris

 

 

 

 

 

Link to comment
Share on other sites

In CC5214, getCategoryProducts() simply fetches a list of product_id values:

Line 739-741: foreach ($result as $product) { $list[] = $product['product_id']; }

which is then given to getProductData(). Also given to getProductData() is the $order to be used:

Lines 729-738: $order['column_name'] = 'direction'

 

Part of determining the sorted order is based on the Store Setting, Layout tab, Product List Sorting. This is overridden when the customer chooses to use the sorter according to their needs.

 

In getProductData(), $order is tested for 'price' and if so, is adjusted to include sale prices. Otherwise $order is sent unchanged to Database->select(). There, $order is expanded to build the correct ORDER BY phrase.

 

So, without destroying the customer's ability to set the sort, we can try this in getCategoryProducts(), line 734-738:

Was:
} else {
  $order_column = $GLOBALS['config']->get('config', 'product_sort_column');
  $order_direction = $GLOBALS['config']->get('config', 'product_sort_direction');
  $order[$order_column] = $order_direction;
}
 
Now:
} else {
  // $order_column = $GLOBALS['config']->get('config', 'product_sort_column');
  // $order_direction = $GLOBALS['config']->get('config', 'product_sort_direction');
  // $order[$order_column] = $order_direction;
  $order = array('stock_level'=>'DESC', 'popularity'=>'DESC');
}

But this sorts according to main stock levels, not the general test if a product is simply "in stock".

Link to comment
Share on other sites

Cracked it. My categories now sort in a logical order - with "in stock" products being shown first. And the normal sort function remains intact.

 

I made the adjustment that you suggested BSmither, then made this change to the getProductData() function:

		if (isset($order['price']) && $GLOBALS['config']->get('config', 'catalogue_sale_mode')) {
			if(!empty($page) && is_numeric($page)){
				$query = 'SELECT *, IF(`sale_price` > 0, `sale_price`, `price`) AS price_sort FROM '.$GLOBALS['config']->get('config', 'dbprefix').'CubeCart_inventory WHERE '.$where.' ORDER BY `price_sort` '.$order['price'].' LIMIT '.$per_page.' OFFSET '.(int)($page-1)*$per_page;
			} else {
				$query = 'SELECT *, IF(`sale_price` > 0, `sale_price`, `price`) AS price_sort FROM '.$GLOBALS['config']->get('config', 'dbprefix').'CubeCart_inventory WHERE '.$where.' ORDER BY `price_sort` '.$order['price'];
			}
			$result = $GLOBALS['db']->query($query);
		//CHRIS edit - 29/11/14
    } elseif (isset($order['stock_level'])){
      $result = $GLOBALS['db']->select('CubeCart_inventory', false, $where, "`CubeCart_inventory`.`stock_level`=0,`CubeCart_inventory`.`popularity` DESC", $per_page, $page);
    }  else {
			$result = $GLOBALS['db']->select('CubeCart_inventory', false, $where, $order, $per_page, $page);
		}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...