Custom Products sort order field for products in admin/categories.php
This is based on the OSC Contrib by Brian Sim:
brianwsim@hotmail.com.
All credits go to him!
I have only adapted it for use in CRE Loaded V. 6.1a.
If you want to sort your products in a certain way instead of the standard alphabetical order, this is for you!!
This modification adds a textbox next to the status radio buttons in categories.php which allows you to enter a default sort order for your products.
The customer is still able to resort products in the product listing if they want but this mod provides a means of ensuring that your premium products are shown at the top of the listing.
To set the sort order, simply enter the number directly into the textbox. Note, you must highlight (right-click) the value already in the textbox in order to change it. Simply clicking in the textbox will take you to the product_preview page.
It is easy to implement with only one column added to the products table and 8 chunks of code to copy/paste.
To implement this mod, follow the instructions below:
Also make the following database change (using phpmyadmin or similar tool)
Code:
ALTER TABLE products ADD products_sort_order INT( 4 ) DEFAULT '0' NOT NULL;
Now copy/paste the following into the relevant files:
==========================================================
catalog/Index.php
LOOK FOR:
Code:
$sort_column = CATEGORIES_SORT_ORDER;
$sort_order = 'a';
} else {
$sort_col = substr($HTTP_GET_VARS['sort'], 0 , 1);
$sort_column = $column_list[$sort_col-1];
$sort_order = substr($HTTP_GET_VARS['sort'], 1);
}
$listing_sql .= ' order by ';
switch ($sort_column) {
case 'PRODUCT_LIST_MODEL':
$listing_sql .= "p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
case 'PRODUCT_LIST_NAME':
$listing_sql .= "pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
break;
case 'PRODUCT_LIST_MANUFACTURER':
$listing_sql .= "m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
case 'PRODUCT_LIST_QUANTITY':
$listing_sql .= "p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
case 'PRODUCT_LIST_IMAGE':
$listing_sql .= "pd.products_name";
break;
case 'PRODUCT_LIST_WEIGHT':
$listing_sql .= "p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
case 'PRODUCT_LIST_PRICE':
$listing_sql .= "final_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
}
REPLACE WITH:
Code:
//Custom Sort Order for Products
// $sort_column = CATEGORIES_SORT_ORDER;
// $sort_order = 'a';
for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
if ($column_list[$i] == 'PRODUCT_LIST_NAME') {
$HTTP_GET_VARS['sort'] = $i+1 . 'a';
$listing_sql .= " order by p.products_sort_order, pd.products_name";
break;
}
}
} else {
$sort_col = substr($HTTP_GET_VARS['sort'], 0 , 1);
// $sort_column = $column_list[$sort_col-1];
$sort_order = substr($HTTP_GET_VARS['sort'], 1);
// }
$listing_sql .= ' order by ';
// switch ($sort_column) {
switch ($column_list[$sort_col-1]) {
case 'PRODUCT_LIST_MODEL':
$listing_sql .= "p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
case 'PRODUCT_LIST_NAME':
$listing_sql .= "p.products_sort_order, pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
//$listing_sql .= "pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
break;
case 'PRODUCT_LIST_MANUFACTURER':
$listing_sql .= "m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
case 'PRODUCT_LIST_QUANTITY':
$listing_sql .= "p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
case 'PRODUCT_LIST_IMAGE':
$listing_sql .= "pd.products_name";
break;
case 'PRODUCT_LIST_WEIGHT':
$listing_sql .= "p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
case 'PRODUCT_LIST_PRICE':
$listing_sql .= "final_price " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
break;
}
}
//End of Custom Sort Order for Products
==========================================================
catalog/admin/categories.php
After
Code:
if (tep_not_null($action)) {
switch ($action) {
Add
Code:
//Custom Sort Order for Products
case 'setsortorder':
if (isset($HTTP_GET_VARS['pID'])) {
tep_set_product_sort_order($HTTP_GET_VARS['pID'], $HTTP_GET_VARS['sortorder']);
}
tep_redirect(tep_href_link(FILENAME_CATEGORIES, 'cPath=' . $HTTP_GET_VARS['cPath'] . '&pID=' . $HTTP_GET_VARS['pID']));
break;
//End of Custom Sort Order for Products
---------------------------------------------------------------------
Before
Code:
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?> </td>
Add
Code:
<!--Custom Sort Order Field for Products-->
<td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_SORT_ORDER; ?></td>
<!--End of Custom Sort Order Field for Products-->
---------------------------------------------------------------------
After
Code:
<td class="dataTableContent" align="center"> </td>
Add
Code:
<!--Custom sort order field for products-->
<td class="dataTableContent" align="center"></td>
<!--End of Custom sort order field for products-->
---------------------------------------------------------------------
Replace
Code:
$products_count = 0;
if (isset($HTTP_GET_VARS['search'])) {
$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_status, p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and pd.products_name like '%" . tep_db_input($search) . "%' order by pd.products_name");
} else {
$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_status from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$current_category_id . "' order by pd.products_name");
}
With
Code:
$products_count = 0;
if (isset($HTTP_GET_VARS['search'])) {
//Custom sort order field for products
// $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_status, p.products_model, p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and pd.products_name like '%" . tep_db_input($search) . "%' order by pd.products_name");
$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_status, p.products_sort_order, p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and pd.products_name like '%" . tep_db_input($search) . "%' order by pd.products_name");
} else {
// $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_status, p.products_model from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$current_category_id . "' order by pd.products_name");
$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_quantity, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_status, p.products_sort_order from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and p2c.categories_id = '" . (int)$current_category_id . "' order by pd.products_name");
//End of Custom sort order field for products
---------------------------------------------------------------------
Before
Code:
<td class="dataTableContent" align="right"><?php if (isset($pInfo) && is_object($pInfo) && ($products['products_id'] == $pInfo->products_id)) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(FILENAME_CATEGORIES, 'cPath=' . $cPath . '&pID=' . $products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?> </td>
</tr>
<?php
Add
Code:
<!--Custom sort order field for products-->
<td class="dataTableContent" align="center"><?php echo '<form name="setsortorder" action="' . tep_href_link(FILENAME_CATEGORIES); ?>">
<input type="hidden" name="action" value="setsortorder">
<input type="hidden" name="cPath" value="<?php echo $cPath; ?>">
<input type="hidden" name="pID" value="<?php echo $products['products_id']; ?>">
<?php echo tep_draw_input_field('sortorder', $products['products_sort_order'], 'SIZE=3 maxlength=3 onchange="this.form.submit();"');?></td></form>
<!--End of Custom sort order field for products-->
==========================================================
catalog/admin/includes/functions/general.php
Before
Code:
////
// Sets the status of a product
function tep_set_product_status($products_id, $status) {
Add
Code:
////Custom sort order field for products
// Sets the sort order of a product
function tep_set_product_sort_order($products_id, $sort_order) {
return tep_db_query("update " . TABLE_PRODUCTS . " set products_sort_order = '" . $sort_order . "', products_last_modified = now() where products_id = '" . (int)$products_id . "'");
}
//End of Custom sort order field for products
==========================================================
catalog/admin/includes/languages/english/categories.php
(AND ANY OTHER LANGUAGES)
After
Code:
define('TABLE_HEADING_STATUS', 'Status');
Add
Code:
//Custom sort order field for products
define('TABLE_HEADING_SORT_ORDER', 'Sort Order');
define('TABLE_HEADING_IMAGE', 'Image');
//End of Custom sort order field for products
==========================================================