Forums / Setting Up Categories, Products, Attributes / Show out of stock items last

Show out of stock items last

Views: 2,941

Results 1 to 16 of 16
1 Jan 2012, 12:32 AM
#1
matchlock avatar

matchlock

New Zenner

Join Date:
Jun 2011
Posts:
92
Plugin Contributions:
0

Show out of stock items last

I have searched all over and have been unable to find the answer to this question. How do I show sold out items after in stock items.

For example if you go to http://www.matchlockarms.com/store/index.php?main_page=index&manufacturers_id=10

you will see that the first two listed products are out of stock. I'd like these to be listed after in stock items. Any guidance would be greatly appreciated.

Happy New Year!

Owner - MatchLock Arms, LLC and M&M Toys (Yes I sell toys and guns, but not toy guns)
http://www.matchlockarms.com
http://www.mandmtoys.com

13 Jan 2012, 2:42 AM
#2
matchlock avatar

matchlock

New Zenner

Join Date:
Jun 2011
Posts:
92
Plugin Contributions:
0

Re: Show out of stock items last

Anyone? This has to be a common request but I can't find anything.

Owner - MatchLock Arms, LLC and M&M Toys (Yes I sell toys and guns, but not toy guns)
http://www.matchlockarms.com
http://www.mandmtoys.com

13 Jan 2012, 2:54 AM
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Show out of stock items last

Strange, I can't remember the last time I saw someone ask for that. It's not a built-in feature. You can do it yourself though, if you've got the PHP and SQL skills.

You'll need to dig deep into several complicated parts of code and make it run special database queries to sort the data in a different order. Perhaps even running two separate queries (one for in-stock, followed by one for out-of-stock) and then merge them together, before showing results back to the storefront.

How are your PHP and SQL skills?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

23 Jan 2013, 8:25 PM
#4
wwrepair avatar

wwrepair

New Zenner

Join Date:
Jun 2012
Posts:
92
Plugin Contributions:
0

Re: Show out of stock items last

Did we ever get an answer to this? I also would like to have my in stock products show first and have my out of stock items last.

24 Jan 2013, 12:18 AM
#5
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Show out of stock items last

For the Product Listing via Category you can edit a few files to make this work ...

Edit the file:
/includes/modules/pages/index/main_template_vars.php

and around line 183 add the code in RED:

  // always add quantity regardless of whether or not it is in the listing for add to cart buttons
  if (PRODUCT_LIST_QUANTITY < 1) {
    $select_column_list .= 'p.products_quantity, ';
  }

[B]$select_column_list .= '(products_quantity > 0) AS sort_sold, ';[/B]

Next, edit the file:
/includes/init_filters/default_filter.php

And you need to add to all of the lines that start with:
$listing_sql .= " order by

the code:
sort_sold DESC,

for example:

          $listing_sql .= " order by p.products_sort_order, pd.products_name";

would become:

          $listing_sql .= " order by[B] sort_sold DESC,[/B] p.products_sort_order, pd.products_name";

Note: if you use Music Product Types, you will need to do something similar to the other two files in:
/includes/init_filters

Note: you will also need to do something similar for the Advanced Search files to have this sort change ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

24 Jan 2013, 4:16 PM
#6
wwrepair avatar

wwrepair

New Zenner

Join Date:
Jun 2012
Posts:
92
Plugin Contributions:
0

Re: Show out of stock items last

Thank Ajeh, I always appreciate your help!
I tried this but it didn't quite work. When I uploaded the new main_template_vars.php file it wouldn't display my home screen. When I uploaded the default_filter.php file with the changes my website wouldn't load at all. I didn't make any changes to the advanced search yet. Do you know what may have happened? I have restored my site with a back up file. I do not do anything with music files - I just sell appliance parts. :laugh:

Thank you in advance for your support - you have helped me on many occasions, thanks again!

24 Jan 2013, 4:42 PM
#7
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Show out of stock items last

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

26 Jan 2013, 12:31 AM
#8
wwrepair avatar

wwrepair

New Zenner

Join Date:
Jun 2012
Posts:
92
Plugin Contributions:
0

Re: Show out of stock items last

Ajeh:

What do your debug logs read?

Okay, I feel really stupid...I have no idea how to check my debug files...ya - that's sad. I obviously have a lot to learn

2 Apr 2013, 4:18 PM
#9
wwrepair avatar

wwrepair

New Zenner

Join Date:
Jun 2012
Posts:
92
Plugin Contributions:
0

Re: Show out of stock items last

Hello,
Okay, I tried this again and I couldn't find the init_filters folder in includes, but found I had to upload to /includes/index_filters/MY_TEMPLATE/default_filter.php instead of /includes/index_filters/default_filter.php. Assuming you meant index_filters instead of init_filters I might be on the right track. I added sort_sold DESC, to every line that started with $listing_sql. Thanks again for any help you can provide!!

The error I get in debug is this: ```php
[02-Apr-2013 10:12:21] PHP Fatal error: 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC, p.products_id, p.products_type, p.master_categories_id, p.manufacturers_id' at line 1 :: select sort_sold DESC, p.products_id, p.products_type, p.master_categories_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status =1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status
from znc_products_description pd, znc_products p left join znc_manufacturers m on p.manufacturers_id = m.manufacturers_id, znc_products_to_categories p2c left join znc_specials s on p2c.products_id = s.products_id
where p.products_status = 1
and p.products_id = p2c.products_id
and pd.products_id = p2c.products_id
and pd.language_id = '1'
and p2c.categories_id = '179' in /home8/wwrepair/public_html/parts/includes/classes/db/mysql/query_factory.php on line 101


Also, I did not get an error when I uploaded the main_template_vars.php this time. Just after the default_filter.php.
3 Apr 2013, 4:31 PM
#10
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Show out of stock items last

It looks like you may have issues on how you changed the file:
/includes/index_filters/default_filter.php

try the code:

<?php
/**
 * default_filter.php  for index filters
 *
 * index filter for the default product type
 * show the products of a specified manufacturer
 *
 * @package productTypes
 * @copyright Copyright 2003-2009 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @todo Need to add/fine-tune ability to override or insert entry-points on a per-product-type basis
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: default_filter.php 14870 2009-11-19 22:36:24Z drbyte $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}
if (isset($_GET['sort']) && strlen($_GET['sort']) > 3) {
  $_GET['sort'] = substr($_GET['sort'], 0, 3);
}
if (isset($_GET['alpha_filter_id']) && (int)$_GET['alpha_filter_id'] > 0) {
    $alpha_sort = " and pd.products_name LIKE '" . chr((int)$_GET['alpha_filter_id']) . "%' ";
  } else {
    $alpha_sort = '';
  }
  if (!isset($select_column_list)) $select_column_list = "";
   // show the products of a specified manufacturer
  if (isset($_GET['manufacturers_id']) && $_GET['manufacturers_id'] != '' ) {
    if (isset($_GET['filter_id']) && zen_not_null($_GET['filter_id'])) {
// We are asked to show only a specific category
      $listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.master_categories_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, if(s.status = 1, s.specials_new_products_price, NULL) AS specials_new_products_price, IF(s.status = 1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status
       from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id , " .
       TABLE_PRODUCTS_DESCRIPTION . " pd, " .
       TABLE_MANUFACTURERS . " m, " .
       TABLE_PRODUCTS_TO_CATEGORIES . " p2c
       where p.products_status = 1
         and p.manufacturers_id = m.manufacturers_id
         and m.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'
         and p.products_id = p2c.products_id
         and pd.products_id = p2c.products_id
         and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
         and p2c.categories_id = '" . (int)$_GET['filter_id'] . "'" .
         $alpha_sort;
    } else {
// We show them all
      $listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.master_categories_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status = 1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status
      from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " .
      TABLE_PRODUCTS_DESCRIPTION . " pd, " .
      TABLE_MANUFACTURERS . " m
      where p.products_status = 1
        and pd.products_id = p.products_id
        and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
        and p.manufacturers_id = m.manufacturers_id
        and m.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'" .
        $alpha_sort;
    }
  } else {
// show the products in a given category
    if (isset($_GET['filter_id']) && zen_not_null($_GET['filter_id'])) {
// We are asked to show only specific category
      $listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.master_categories_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status = 1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status
      from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " .
      TABLE_PRODUCTS_DESCRIPTION . " pd, " .
      TABLE_MANUFACTURERS . " m, " .
      TABLE_PRODUCTS_TO_CATEGORIES . " p2c
      where p.products_status = 1
        and p.manufacturers_id = m.manufacturers_id
        and m.manufacturers_id = '" . (int)$_GET['filter_id'] . "'
        and p.products_id = p2c.products_id
        and pd.products_id = p2c.products_id
        and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
        and p2c.categories_id = '" . (int)$current_category_id . "'" .
        $alpha_sort;
    } else {
// We show them all
      $listing_sql = "select " . $select_column_list . " p.products_id, p.products_type, p.master_categories_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, pd.products_description, IF(s.status = 1, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status =1, s.specials_new_products_price, p.products_price) as final_price, p.products_sort_order, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status
       from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " .
       TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id, " .
       TABLE_PRODUCTS_TO_CATEGORIES . " p2c left join " . TABLE_SPECIALS . " s on p2c.products_id = s.products_id
       where p.products_status = 1
         and p.products_id = p2c.products_id
         and pd.products_id = p2c.products_id
         and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
         and p2c.categories_id = '" . (int)$current_category_id . "'" .
         $alpha_sort;
    }
  }

// set the default sort order setting from the Admin when not defined by customer
  if (!isset($_GET['sort']) and PRODUCT_LISTING_DEFAULT_SORT_ORDER != '') {
    $_GET['sort'] = PRODUCT_LISTING_DEFAULT_SORT_ORDER;
  }

  if (isset($column_list)) {
    if ((!isset($_GET['sort'])) || (isset($_GET['sort']) && !preg_match('/[1-8][ad]/', $_GET['sort'])) || (substr($_GET['sort'], 0, 1) > sizeof($column_list)) ) {
      for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
        if (isset($column_list[$i]) && $column_list[$i] == 'PRODUCT_LIST_NAME') {
          $_GET['sort'] = $i+1 . 'a';
//          $listing_sql .= " order by p.products_sort_order, pd.products_name";
          $listing_sql .= " order by sort_sold DESC, p.products_sort_order, pd.products_name";
          break;
        } else {
// sort by products_sort_order when PRODUCT_LISTING_DEFAULT_SORT_ORDER is left blank
// for reverse, descending order use:
//       $listing_sql .= " order by p.products_sort_order desc, pd.products_name";
          $listing_sql .= " order by sort_sold DESC, p.products_sort_order, pd.products_name";
          break;
        }
      }
// if set to nothing use products_sort_order and PRODUCTS_LIST_NAME is off
      if (PRODUCT_LISTING_DEFAULT_SORT_ORDER == '') {
        $_GET['sort'] = '20a';
      }
    } else {
      $sort_col = substr($_GET['sort'], 0 , 1);
      $sort_order = substr($_GET['sort'], 1);
      switch ($column_list[$sort_col-1]) {
        case 'PRODUCT_LIST_MODEL':
//          $listing_sql .= " order by p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          $listing_sql .= " order by sort_sold DESC, p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
        case 'PRODUCT_LIST_NAME':
//          $listing_sql .= " order by pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
          $listing_sql .= " order by sort_sold DESC, sort_sold, pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
          break;
        case 'PRODUCT_LIST_MANUFACTURER':
//          $listing_sql .= " order by m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          $listing_sql .= " order by sort_sold DESC, m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
        case 'PRODUCT_LIST_QUANTITY':
//          $listing_sql .= " order by p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          $listing_sql .= " order by sort_sold DESC, p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
        case 'PRODUCT_LIST_IMAGE':
//          $listing_sql .= " order by pd.products_name";
          $listing_sql .= " order by sort_sold DESC, pd.products_name";
          break;
        case 'PRODUCT_LIST_WEIGHT':
//          $listing_sql .= " order by p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          $listing_sql .= " order by sort_sold DESC, p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
        case 'PRODUCT_LIST_PRICE':
//          $listing_sql .= " order by p.products_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          $listing_sql .= " order by sort_sold DESC, p.products_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
          break;
      }
    }
  }
// optional Product List Filter
  if (PRODUCT_LIST_FILTER > 0) {
    if (isset($_GET['manufacturers_id']) && $_GET['manufacturers_id'] != '') {
      $filterlist_sql = "select distinct c.categories_id as id, cd.categories_name as name
      from " . TABLE_PRODUCTS . " p, " .
      TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " .
      TABLE_CATEGORIES . " c, " .
      TABLE_CATEGORIES_DESCRIPTION . " cd
      where p.products_status = 1
        and p.products_id = p2c.products_id
        and p2c.categories_id = c.categories_id
        and p2c.categories_id = cd.categories_id
        and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
        and p.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'
      order by cd.categories_name";
    } else {
      $filterlist_sql= "select distinct m.manufacturers_id as id, m.manufacturers_name as name
      from " . TABLE_PRODUCTS . " p, " .
      TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " .
      TABLE_MANUFACTURERS . " m
      where p.products_status = 1
        and p.manufacturers_id = m.manufacturers_id
        and p.products_id = p2c.products_id
        and p2c.categories_id = '" . (int)$current_category_id . "'
      order by m.manufacturers_name";
    }
    $do_filter_list = false;
    $filterlist = $db->Execute($filterlist_sql);
    if ($filterlist->RecordCount() > 1) {
        $do_filter_list = true;
      if (isset($_GET['manufacturers_id'])) {
        $getoption_set =  true;
        $get_option_variable = 'manufacturers_id';
        $options = array(array('id' => '', 'text' => TEXT_ALL_CATEGORIES));
      } else {
        $options = array(array('id' => '', 'text' => TEXT_ALL_MANUFACTURERS));
      }
      while (!$filterlist->EOF) {
        $options[] = array('id' => $filterlist->fields['id'], 'text' => $filterlist->fields['name']);
        $filterlist->MoveNext();
      }
    }
  }

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

3 Apr 2013, 4:40 PM
#11
wwrepair avatar

wwrepair

New Zenner

Join Date:
Jun 2012
Posts:
92
Plugin Contributions:
0

Re: Show out of stock items last

FANTASTIC!!! I did have to upload it to /includes/index_filters/MY_TEMPLATE/default.php. Works perfect!!! I will remember you when I am filthy rich and famous! Thanks again, you are a genius!

3 Apr 2013, 6:01 PM
#12
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Show out of stock items last

Thanks you for the update that this is working for you and looking forward to your prosperous success! :cool:

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

5 Apr 2013, 5:12 PM
#13
wwrepair avatar

wwrepair

New Zenner

Join Date:
Jun 2012
Posts:
92
Plugin Contributions:
0

Re: Show out of stock items last

Ajeh,
You mentioned previously that I should make a similar adjustment to the Advanced search results. Could you possibly tell me what to change? I can find and make changes in the php if you tell me where to go. I always make a copy and change them in my custom folder. Any suggestions would be greatly appreciated. This is not an urgent matter, so whenever you are able to share your wisdom I will be patiently waiting :D

6 Apr 2013, 2:57 AM
#14
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Show out of stock items last

I believe for the Advanced Search Results you need the header_php.php read like the attached ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

8 Apr 2013, 4:21 PM
#15
wwrepair avatar

wwrepair

New Zenner

Join Date:
Jun 2012
Posts:
92
Plugin Contributions:
0

Re: Show out of stock items last

Ajeh:

I believe for the Advanced Search Results you need the header_php.php read like the attached ...

Once again you are fabulous :hug: Worked like a charm

8 Apr 2013, 4:25 PM
#16
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Show out of stock items last

Thanks for the update that this works for you on the Sold Out Products displaying last on a search ... :smile:

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!