Advertisement
Guest User

Zen Cart Default Filter with sort by recommendation

a guest
Nov 17th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.33 KB | None | 0 0
  1. <?php
  2. /**
  3.  * default_filter.php  for index filters
  4.  *
  5.  * index filter for the default product type
  6.  * show the products of a specified manufacturer
  7.  *
  8.  * @package productTypes
  9.  * @copyright Copyright 2003-2009 Zen Cart Development Team
  10.  * @copyright Portions Copyright 2003 osCommerce
  11.  * @todo Need to add/fine-tune ability to override or insert entry-points on a per-product-type basis
  12.  * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  13.  * @version $Id: default_filter.php 14870 2009-11-19 22:36:24Z drbyte $
  14.  */
  15. if (!defined('IS_ADMIN_FLAG')) {
  16.   die('Illegal Access');
  17. }
  18. if (isset($_GET['sort']) && strlen($_GET['sort']) > 3) {
  19.   $_GET['sort'] = substr($_GET['sort'], 0, 3);
  20. }
  21. if (isset($_GET['alpha_filter_id']) && (int)$_GET['alpha_filter_id'] > 0) {
  22.     $alpha_sort = " and pd.products_name LIKE '" . chr((int)$_GET['alpha_filter_id']) . "%' ";
  23.   } else {
  24.     $alpha_sort = "";
  25.   }
  26.  
  27. //BOF: Recommended Products 01 of 04  
  28. if ($_GET['sort'] == "red") {
  29.   $date_parameters = "and osh.date_added >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY";
  30. } else {
  31.   $date_parameters = "";
  32. }
  33. //EOF: Recommended Products 01 0f 04
  34.  
  35.   //BOF product listing filters
  36.   $filterCond = "";
  37.   if (function_exists('getFilterWhereCond'))
  38.   {
  39.     $filterCond = getFilterWhereCond();
  40.   }
  41.   //EOF product listing filters
  42.   if (!isset($select_column_list)) $select_column_list = "";
  43.    // show the products of a specified manufacturer
  44.   if (isset($_GET['manufacturers_id']) && $_GET['manufacturers_id'] != '' ) {
  45.     if (isset($_GET['filter_id']) && zen_not_null($_GET['filter_id'])) {
  46. // We are asked to show only a specific category
  47.       $listing_sql = "select " . $select_column_list . " p.products_id, COUNT(opc.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
  48.       from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id , "
  49.        //BOF: Recommended Products 02 of 04
  50.        . TABLE_ORDERS_STATUS_HISTORY . " osh left join " . TABLE_ORDERS_TOTAL . " ot on osh.orders_id = ot.orders_id, "
  51.        . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_ORDERS_PRODUCTS . " opc on pd.products_id = opc.products_id, "
  52.        //EOF: Recommended Products 02 of 04
  53.        . TABLE_MANUFACTURERS . " m, "
  54.        . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
  55.       where p.products_status = 1 "
  56.      . $date_parameters .
  57.          " and p.manufacturers_id = m.manufacturers_id
  58.         and m.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'
  59.         and p.products_id = p2c.products_id
  60.         and pd.products_id = p2c.products_id
  61.         and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  62.         and p2c.categories_id = '" . (int)$_GET['filter_id'] . "'" .
  63.          $alpha_sort
  64.      //BOF: Recommended Products 03 of 04  
  65.      . " GROUP BY p.products_id ";
  66.      //BOF: Recommended Products 03 of 04
  67.     } else {
  68. // We show them all
  69.       $listing_sql = "select " . $select_column_list . " p.products_id, COUNT(opc.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
  70.      from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, "
  71.       //BOF: Recommended Products 02 of 04
  72.       . TABLE_ORDERS_STATUS_HISTORY . " osh left join " . TABLE_ORDERS_TOTAL . " ot on osh.orders_id = ot.orders_id, "
  73.       . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_ORDERS_PRODUCTS . " opc on pd.products_id = opc.products_id, "
  74.       //EOF: Recommended Products 02 of 04
  75.       . TABLE_MANUFACTURERS . " m
  76.      where p.products_status = 1 "
  77.     . $date_parameters .
  78.         " and pd.products_id = p.products_id
  79.        and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  80.        and p.manufacturers_id = m.manufacturers_id
  81.        and m.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'" .
  82.         $alpha_sort
  83.      //BOF: Recommended Products 03 of 04  
  84.      . " GROUP BY p.products_id ";
  85.      //BOF: Recommended Products 03 of 04
  86.     }
  87.   } else {
  88. // show the products in a given category
  89.     if (isset($_GET['filter_id']) && zen_not_null($_GET['filter_id'])) {
  90. // We are asked to show only specific category
  91.       $listing_sql = "select " . $select_column_list . " p.products_id, COUNT(opc.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
  92.      from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, "
  93.       //BOF: Recommended Products 02 of 04
  94.       . TABLE_ORDERS_STATUS_HISTORY . " osh left join " . TABLE_ORDERS_TOTAL . " ot on osh.orders_id = ot.orders_id, "
  95.       . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_ORDERS_PRODUCTS . " opc on pd.products_id = opc.products_id, "
  96.       //EOF: Recommended Products 02 of 04
  97.       . TABLE_MANUFACTURERS . " m, "
  98.       . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
  99.      where p.products_status = 1 "
  100.     . $date_parameters .
  101.         " and p.manufacturers_id = m.manufacturers_id
  102.        and m.manufacturers_id = '" . (int)$_GET['filter_id'] . "'
  103.        and p.products_id = p2c.products_id
  104.        and pd.products_id = p2c.products_id
  105.        and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  106.        and p2c.categories_id = '" . (int)$current_category_id . "'" .
  107.         $alpha_sort
  108.      //BOF: Recommended Products 03 of 04  
  109.      . " GROUP BY p.products_id ";
  110.      //BOF: Recommended Products 03 of 04  
  111.     } else {
  112. // We show them all
  113.       $listing_sql = "select " . $select_column_list . " p.products_id, COUNT(opc.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
  114.       from "
  115.        //BOF: Recommended Products 02 of 04
  116.        . TABLE_ORDERS_STATUS_HISTORY . " osh left join " . TABLE_ORDERS_TOTAL . " ot on osh.orders_id = ot.orders_id, "
  117.        . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_ORDERS_PRODUCTS . " opc on pd.products_id = opc.products_id, "
  118.        //EOF: Recommended Products 02 of 04
  119.        . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id, "
  120.        . TABLE_PRODUCTS_TO_CATEGORIES . " p2c left join " . TABLE_SPECIALS . " s on p2c.products_id = s.products_id
  121.      
  122.       " . (($filterCond != "") ? " LEFT JOIN " . TABLE_PRODUCTS_EXTRA_INFO . " pe ON p2c.products_id = pe.products_id " : "") . "
  123.       where p.products_status = 1 "
  124.      . $date_parameters .
  125.          " and p.products_id = p2c.products_id
  126.         and pd.products_id = p2c.products_id
  127.         and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  128.         and p2c.categories_id = '" . (int)$current_category_id . "'" .
  129.          $alpha_sort .
  130.          $filterCond
  131.      //BOF: Recommended Products 03 of 04
  132.      . " GROUP BY p.products_id ";
  133.      //BOF: Recommended Products 03 of 04
  134.     }
  135.   }
  136.  
  137. // set the default sort order setting from the Admin when not defined by customer
  138.   if (!isset($_GET['sort']) and PRODUCT_LISTING_DEFAULT_SORT_ORDER != '') {
  139.     $_GET['sort'] = PRODUCT_LISTING_DEFAULT_SORT_ORDER;
  140.   }
  141.  
  142.   //bof customer defined sort order of results
  143.   if (isset($_POST['resultsSortOrder']) && $_POST['resultsSortOrder'] != '') {
  144.     $_GET['sort'] = $_SESSION['resultsSortOrder'] = zen_db_input($_POST['resultsSortOrder']);
  145.   } elseif (isset($_GET['sort']) && $_GET['sort'] != '') {
  146.     $_GET['sort'] = $_SESSION['resultsSortOrder'] = zen_db_input($_GET['sort']);
  147.   } elseif (isset($_SESSION['resultsSortOrder']) && $_SESSION['resultsSortOrder'] != '') {
  148.     $_GET['sort'] = $_SESSION['resultsSortOrder'];
  149.   }
  150.   //eof customer defined sort order of results
  151.  
  152.   if (isset($column_list)) {
  153.     if ((!isset($_GET['sort'])) || (isset($_GET['sort']) && !preg_match('/[1-8][ad]/', $_GET['sort'])) || (substr($_GET['sort'], 0, 1) > sizeof($column_list)) ) {
  154.       for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
  155.     //BOF: Recommended Products 04 of 04  
  156.         if ($_GET['sort'] == "red") {
  157.           $listing_sql .= " ORDER BY COUNT(opc.products_id) DESC";
  158.           break;
  159.     //BOF: Recommended Products 04 of 04  
  160.         } elseif (isset($column_list[$i]) && $column_list[$i] == 'PRODUCT_LIST_NAME') {
  161.           $_GET['sort'] = $i+1 . 'a';
  162.           $listing_sql .= " order by p.products_sort_order, pd.products_name";
  163.           break;
  164.           //EOF: Recommended Products 04 of 04
  165.         } else {
  166. // sort by products_sort_order when PRODUCT_LISTING_DEFAULT_SORT_ORDER is left blank
  167. // for reverse, descending order use:
  168. //       $listing_sql .= " order by p.products_sort_order desc, pd.products_name";
  169.           $listing_sql .= " order by p.products_sort_order, pd.products_name";
  170.           break;
  171.         }
  172.       }
  173. // if set to nothing use products_sort_order and PRODUCTS_LIST_NAME is off
  174.       if (PRODUCT_LISTING_DEFAULT_SORT_ORDER == '') {
  175.         $_GET['sort'] = '20a';
  176.       }
  177.     } else {
  178.       $sort_col = substr($_GET['sort'], 0 , 1);
  179.       $sort_order = substr($_GET['sort'], 1);
  180.       switch ($column_list[$sort_col-1]) {
  181.         case 'PRODUCT_LIST_MODEL':
  182.           $listing_sql .= " order by p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  183.           break;
  184.         case 'PRODUCT_LIST_NAME':
  185.           $listing_sql .= " order by pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
  186.           break;
  187.         case 'PRODUCT_LIST_MANUFACTURER':
  188.           $listing_sql .= " order by m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  189.           break;
  190.         case 'PRODUCT_LIST_QUANTITY':
  191.           $listing_sql .= " order by p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  192.           break;
  193.         case 'PRODUCT_LIST_IMAGE':
  194.           $listing_sql .= " order by pd.products_name";
  195.           break;
  196.         case 'PRODUCT_LIST_WEIGHT':
  197.           $listing_sql .= " order by p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  198.           break;
  199.         case 'PRODUCT_LIST_PRICE':
  200.           $listing_sql .= " order by p.products_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  201.           break;
  202.       }
  203.     }
  204.   }
  205.   if (($_GET['sort'] == "red") && (!isset($column_list))) {
  206.     $listing_sql .= " ORDER BY COUNT(opc.products_id) DESC";
  207.   }
  208. // optional Product List Filter
  209.   if (PRODUCT_LIST_FILTER > 0) {
  210.     if (isset($_GET['manufacturers_id']) && $_GET['manufacturers_id'] != '') {
  211.       $filterlist_sql = "select distinct c.categories_id as id, cd.categories_name as name
  212.      from " . TABLE_PRODUCTS . " p, " .
  213.       TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " .
  214.       TABLE_CATEGORIES . " c, " .
  215.       TABLE_CATEGORIES_DESCRIPTION . " cd
  216.      where p.products_status = 1
  217.        and p.products_id = p2c.products_id
  218.        and p2c.categories_id = c.categories_id
  219.        and p2c.categories_id = cd.categories_id
  220.        and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  221.        and p.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'
  222.      order by cd.categories_name";
  223.     } else {
  224.       $filterlist_sql= "select distinct m.manufacturers_id as id, m.manufacturers_name as name
  225.      from " . TABLE_PRODUCTS . " p, " .
  226.       TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " .
  227.       TABLE_MANUFACTURERS . " m
  228.      where p.products_status = 1
  229.        and p.manufacturers_id = m.manufacturers_id
  230.        and p.products_id = p2c.products_id
  231.        and p2c.categories_id = '" . (int)$current_category_id . "'
  232.      order by m.manufacturers_name";
  233.     }
  234.     $do_filter_list = false;
  235.     $filterlist = $db->Execute($filterlist_sql);
  236.     if ($filterlist->RecordCount() > 1) {
  237.         $do_filter_list = true;
  238.       if (isset($_GET['manufacturers_id'])) {
  239.         $getoption_set =  true;
  240.         $get_option_variable = 'manufacturers_id';
  241.         $options = array(array('id' => '', 'text' => TEXT_ALL_CATEGORIES));
  242.       } else {
  243.         $options = array(array('id' => '', 'text' => TEXT_ALL_MANUFACTURERS));
  244.       }
  245.       while (!$filterlist->EOF) {
  246.         $options[] = array('id' => $filterlist->fields['id'], 'text' => $filterlist->fields['name']);
  247.         $filterlist->MoveNext();
  248.       }
  249.     }
  250.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement