Advertisement
Guest User

Zen Cart Default Filter with sort by recommendation

a guest
Nov 17th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.70 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 03  
  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 03
  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.        . TABLE_ORDERS_STATUS_HISTORY . " osh left join " . TABLE_ORDERS_TOTAL . " ot on osh.orders_id = ot.orders_id, "
  50.        . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_ORDERS_PRODUCTS . " opc on pd.products_id = opc.products_id, "
  51.        . TABLE_MANUFACTURERS . " m, "
  52.        . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
  53.       where p.products_status = 1 "
  54.        . $date_parameters .
  55.          " and p.manufacturers_id = m.manufacturers_id
  56.         and m.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'
  57.         and p.products_id = p2c.products_id
  58.         and pd.products_id = p2c.products_id
  59.         and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  60.         and p2c.categories_id = '" . (int)$_GET['filter_id'] . "'" .
  61.          $alpha_sort
  62.          . " GROUP BY p.products_id ";
  63.     } else {
  64. // We show them all
  65.       $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
  66.      from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, "
  67.       . TABLE_ORDERS_STATUS_HISTORY . " osh left join " . TABLE_ORDERS_TOTAL . " ot on osh.orders_id = ot.orders_id, "
  68.       . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_ORDERS_PRODUCTS . " opc on pd.products_id = opc.products_id, "
  69.       . TABLE_MANUFACTURERS . " m
  70.      where p.products_status = 1 "
  71.       . $date_parameters .
  72.         " and pd.products_id = p.products_id
  73.        and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  74.        and p.manufacturers_id = m.manufacturers_id
  75.        and m.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'" .
  76.         $alpha_sort
  77.         . " GROUP BY p.products_id ";
  78.     }
  79.   } else {
  80. // show the products in a given category
  81.     if (isset($_GET['filter_id']) && zen_not_null($_GET['filter_id'])) {
  82. // We are asked to show only specific category
  83.       $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
  84.      from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, "
  85.       . TABLE_ORDERS_STATUS_HISTORY . " osh left join " . TABLE_ORDERS_TOTAL . " ot on osh.orders_id = ot.orders_id, "
  86.       . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_ORDERS_PRODUCTS . " opc on pd.products_id = opc.products_id, "
  87.       . TABLE_MANUFACTURERS . " m, "
  88.       . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
  89.      where p.products_status = 1 "
  90.       . $date_parameters .
  91.         " and p.manufacturers_id = m.manufacturers_id
  92.        and m.manufacturers_id = '" . (int)$_GET['filter_id'] . "'
  93.        and p.products_id = p2c.products_id
  94.        and pd.products_id = p2c.products_id
  95.        and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  96.        and p2c.categories_id = '" . (int)$current_category_id . "'" .
  97.         $alpha_sort
  98.         . " GROUP BY p.products_id ";
  99.     } else {
  100. // We show them all
  101.       $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
  102.       from "
  103.        . TABLE_ORDERS_STATUS_HISTORY . " osh left join " . TABLE_ORDERS_TOTAL . " ot on osh.orders_id = ot.orders_id, "
  104.        . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_ORDERS_PRODUCTS . " opc on pd.products_id = opc.products_id, "
  105.        . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m on p.manufacturers_id = m.manufacturers_id, "
  106.        . TABLE_PRODUCTS_TO_CATEGORIES . " p2c left join " . TABLE_SPECIALS . " s on p2c.products_id = s.products_id
  107.       " . (($filterCond != "") ? " LEFT JOIN " . TABLE_PRODUCTS_EXTRA_INFO . " pe ON p2c.products_id = pe.products_id " : "") . "
  108.       where p.products_status = 1 "
  109.        . $date_parameters .
  110.          " and p.products_id = p2c.products_id
  111.         and pd.products_id = p2c.products_id
  112.         and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  113.         and p2c.categories_id = '" . (int)$current_category_id . "'" .
  114.          $alpha_sort .
  115.          $filterCond
  116.          //BOF: Recommended Products 02 of 03  
  117.          . " GROUP BY p.products_id ";
  118.          //BOF: Recommended Products 02 of 03  
  119.     }
  120.   }
  121.  
  122. // set the default sort order setting from the Admin when not defined by customer
  123.   if (!isset($_GET['sort']) and PRODUCT_LISTING_DEFAULT_SORT_ORDER != '') {
  124.     $_GET['sort'] = PRODUCT_LISTING_DEFAULT_SORT_ORDER;
  125.   }
  126.  
  127.   //bof customer defined sort order of results
  128.   if (isset($_POST['resultsSortOrder']) && $_POST['resultsSortOrder'] != '') {
  129.     $_GET['sort'] = $_SESSION['resultsSortOrder'] = zen_db_input($_POST['resultsSortOrder']);
  130.   } elseif (isset($_GET['sort']) && $_GET['sort'] != '') {
  131.     $_GET['sort'] = $_SESSION['resultsSortOrder'] = zen_db_input($_GET['sort']);
  132.   } elseif (isset($_SESSION['resultsSortOrder']) && $_SESSION['resultsSortOrder'] != '') {
  133.     $_GET['sort'] = $_SESSION['resultsSortOrder'];
  134.   }
  135.   //eof customer defined sort order of results
  136.  
  137.   if (isset($column_list)) {
  138.     if ((!isset($_GET['sort'])) || (isset($_GET['sort']) && !preg_match('/[1-8][ad]/', $_GET['sort'])) || (substr($_GET['sort'], 0, 1) > sizeof($column_list)) ) {
  139.       for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
  140.         //BOF: Recommended Products 03 of 03  
  141.         if ($_GET['sort'] == "red") {
  142.           $listing_sql .= " ORDER BY COUNT(opc.products_id) DESC";
  143.           break;
  144.         //BOF: Recommended Products 03 of 03  
  145.         } elseif (isset($column_list[$i]) && $column_list[$i] == 'PRODUCT_LIST_NAME') {
  146.           $_GET['sort'] = $i+1 . 'a';
  147.           $listing_sql .= " order by p.products_sort_order, pd.products_name";
  148.           break;
  149.         } else {
  150. // sort by products_sort_order when PRODUCT_LISTING_DEFAULT_SORT_ORDER is left blank
  151. // for reverse, descending order use:
  152. //       $listing_sql .= " order by p.products_sort_order desc, pd.products_name";
  153.           $listing_sql .= " order by p.products_sort_order, pd.products_name";
  154.           break;
  155.         }
  156.       }
  157. // if set to nothing use products_sort_order and PRODUCTS_LIST_NAME is off
  158.       if (PRODUCT_LISTING_DEFAULT_SORT_ORDER == '') {
  159.         $_GET['sort'] = '20a';
  160.       }
  161.     } else {
  162.       $sort_col = substr($_GET['sort'], 0 , 1);
  163.       $sort_order = substr($_GET['sort'], 1);
  164.       switch ($column_list[$sort_col-1]) {
  165.         case 'PRODUCT_LIST_MODEL':
  166.           $listing_sql .= " order by p.products_model " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  167.           break;
  168.         case 'PRODUCT_LIST_NAME':
  169.           $listing_sql .= " order by pd.products_name " . ($sort_order == 'd' ? 'desc' : '');
  170.           break;
  171.         case 'PRODUCT_LIST_MANUFACTURER':
  172.           $listing_sql .= " order by m.manufacturers_name " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  173.           break;
  174.         case 'PRODUCT_LIST_QUANTITY':
  175.           $listing_sql .= " order by p.products_quantity " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  176.           break;
  177.         case 'PRODUCT_LIST_IMAGE':
  178.           $listing_sql .= " order by pd.products_name";
  179.           break;
  180.         case 'PRODUCT_LIST_WEIGHT':
  181.           $listing_sql .= " order by p.products_weight " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  182.           break;
  183.         case 'PRODUCT_LIST_PRICE':
  184.           $listing_sql .= " order by p.products_price_sorter " . ($sort_order == 'd' ? 'desc' : '') . ", pd.products_name";
  185.           break;
  186.       }
  187.     }
  188.   }
  189.   if (($_GET['sort'] == "red") && (!isset($column_list))) {
  190.     $listing_sql .= " ORDER BY COUNT(opc.products_id) DESC";
  191.   }
  192. // optional Product List Filter
  193.   if (PRODUCT_LIST_FILTER > 0) {
  194.     if (isset($_GET['manufacturers_id']) && $_GET['manufacturers_id'] != '') {
  195.       $filterlist_sql = "select distinct c.categories_id as id, cd.categories_name as name
  196.      from " . TABLE_PRODUCTS . " p, " .
  197.       TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " .
  198.       TABLE_CATEGORIES . " c, " .
  199.       TABLE_CATEGORIES_DESCRIPTION . " cd
  200.      where p.products_status = 1
  201.        and p.products_id = p2c.products_id
  202.        and p2c.categories_id = c.categories_id
  203.        and p2c.categories_id = cd.categories_id
  204.        and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
  205.        and p.manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'
  206.      order by cd.categories_name";
  207.     } else {
  208.       $filterlist_sql= "select distinct m.manufacturers_id as id, m.manufacturers_name as name
  209.      from " . TABLE_PRODUCTS . " p, " .
  210.       TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " .
  211.       TABLE_MANUFACTURERS . " m
  212.      where p.products_status = 1
  213.        and p.manufacturers_id = m.manufacturers_id
  214.        and p.products_id = p2c.products_id
  215.        and p2c.categories_id = '" . (int)$current_category_id . "'
  216.      order by m.manufacturers_name";
  217.     }
  218.     $do_filter_list = false;
  219.     $filterlist = $db->Execute($filterlist_sql);
  220.     if ($filterlist->RecordCount() > 1) {
  221.         $do_filter_list = true;
  222.       if (isset($_GET['manufacturers_id'])) {
  223.         $getoption_set =  true;
  224.         $get_option_variable = 'manufacturers_id';
  225.         $options = array(array('id' => '', 'text' => TEXT_ALL_CATEGORIES));
  226.       } else {
  227.         $options = array(array('id' => '', 'text' => TEXT_ALL_MANUFACTURERS));
  228.       }
  229.       while (!$filterlist->EOF) {
  230.         $options[] = array('id' => $filterlist->fields['id'], 'text' => $filterlist->fields['name']);
  231.         $filterlist->MoveNext();
  232.       }
  233.     }
  234.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement