Products Listing Sorter v2.0

License: GPL 2.0 as per included license.txt
Version: 2.0, tested on Zen Cart v1.51.
Support Thread: http://www.zen-cart.com/showthread.php?205606-Product-Listing-Sorter-Drop-Down

What does this mod do?

This mod adds a drop-down menu to the product listing and the advanced search results page that can be customised to offer any search options you want. Why? I don't use the column titles on my site design.

This is NOT a filter: all the products are displayed in the chosen order and it works in conjunction with the stock Manufacturer/Category filter drop-down and the Alpha "Sorter" (misnamed as its a filter) drop-down.

The drop-down is enabled by an admin configuration setting:
    Admin->Configuration->Product Listing->Include Product Listing Sorter Dropdown (mod)

Upgrading from a previous version

You should completely uninstall/unmerge changes made by the the previous versions, return all files to default AND then check your site functionality BEFORE installing this version.
This version 2.0 is completely different and is not an enhancement of previous versions.

Installation

This fileset has been tested by dropping directly into a new Zen Cart 1.51 installation. You should test it similarly before integrating it into your own site.

As always, with ANY mod or plugin - BACKUP first and DO NOT INSTALL NOR TEST ON A LIVE/PRODUCTION SITE!

1) Copy/Merge Files (suggest using Beyond Compare)

If any files (the overrides) already exist, you will need to merge the changes. If you are not sure what to do, install this on a new test shop and understand it before merging with your own shop development installation.

  1. \includes\extra_datafiles\product_listing_sorter_filenames.php - NEW (definition of where to find the new code)
  2. \includes\index_filters\YOUR_TEMPLATE\default_filter.php - OVERRIDE (deals with the GET parameters to determine how the product listing should be filtered and sorted)
  3. \includes\languages\english\extra_definitions\product_listing_sorter.php - NEW (english constants and sorting definitions)
  4. OPTIONAL: \includes\languages\spanish\extra_definitions\product_listing_sorter.php - NEW (spanish constants and sorting definitions)
  5. \includes\modules\pages\advanced_search_result\header_php.php - CORE file overwrite, merge differences (creates "order by" sql clause)
  6. \includes\modules\pages\index\main_template_vars.php - CORE file overwrite, merge differences (unset $_GET['multi_sort_id'] when not in use)
  7. \includes\modules\YOUR_TEMPLATE\product_listing.php - OVERRIDE (added GETs to carry filter parameters to product info page)
  8. \includes\modules\YOUR_TEMPLATE\product_listing_sorter.php - OVERRIDE (generates the Product Listing Sorter drop-down code)
  9. \includes\modules\YOUR_TEMPLATE\product_prev_next.php - OVERRIDE (picks up the filtered/sorted sql listing from a SESSION variable to display the same order when using prev/next)
  10. \includes\templates\YOUR_TEMPLATE\templates\tpl_advanced_search_result_default.php - OVERRIDE (template file for the physical location of the Product Listing Sorter drop-down on this page.)
  11. \includes\templates\YOUR_TEMPLATE\templates\tpl_index_product_list.php - OVERRIDE (template file for the physical location of the Product Listing Sorter drop-down, changed to use three drops-downs.)
  12. \includes\templates\YOUR_TEMPLATE\templates\tpl_products_next_previous.php - OVERRIDE (changed to use the filtered/sorted product listing when browsing and re-display the same listing when returning from the product info page.)
  13. includes\application_top.php - CORE file overwrite, merge differences (decontamination of new $_GET['multi_sort_id'])

Copy/merge the files into the correct directories: the files marked with the \YOUR_TEMPLATE path need to go into your template directories or CLASSIC if you are just testing on a new installation, NOT into directories literally called "YOUR_TEMPLATE".

2) Install Admin Configuration Option

Copy the code from \sql\product_listing_sorter_install.sql and paste it into Admin->Install SQL Patches and Send.

This will install a new option in:
    Admin->Configuration->Product Listing->Include Product Listing Sorter Dropdown (mod)

Configuration

Editing the Sort options that appear in the drop-down

 1) the text shown and the corresponding relevant sql clauses are defined in

\includes\languages\english\extra_definitions\product_listing_sorter_en.php

define('PRODUCT_LISTING_SORTER_LIST', '
Name (asc):pd.products_name;
Name (desc):pd.products_name DESC;
Model (asc):p.products_model;
Model (desc):p.products_model DESC;
Price (low to high):p.products_price_sorter;
Price (high to low):p.products_price_sorter DESC;
Manufacturer:m.manufacturers_name;
Newest:p.products_date_added DESC;
Most Popular: p.products_ordered DESC
');

The sql clauses after the  ":" are added onto the sql that produces the product listing, so making a mistake here will give you a blank white page.

Edit or comment out the lines as required.

Debugging

Understanding the sorting and filtering mechanisms was difficult and so I have left many debugging statements in the code. These can be enabled by setting the variable to 1 at the start of the file:

$debug = '0';//1 or 0, show debugging information

I have tested this mod in a new Zen Cart 1.51 installation using the Classic template.

For further debugging I recommend installing the Superglobals plugin to show what variables are being set.

Uninstall

1) Uninstall the Admin Configuration Option

Copy the code from \sql\product_listing_sorter_uninstall.sql and paste it into Admin->Install SQL Patches and Send.

This will remove the option in:
    Admin->Configuration->Product Listing->Include Product Listing Sorter Dropdown (mod)

2) Restore Files

Delete the override files and unmerge the modifications to the core files.

Support

From the community at http://www.zen-cart.com/showthread.php?205606-Product-Listing-Sorter-Drop-Down

-----------------------------------------------------------------------------------------------

Background

I wrote this information to help my goldfish memory.

Zen Cart 1.51 without modifications

In the product listing there are two optional filters available.

1) The Category/Manufacturer filter

Admin option: display category-manufacturer filter

which displays this

product listing category-manufacturer filter

2) The Product Listing Alpha filter (misnamed as an alpha sorter)

Admin option: display alpha filter

which displays this

product listing alpha sorter

Both can be enabled

product listing filters

Default Product Listing Sort Order

This product listing sort order is handled by /includes/index_filters/default_filter.php using the value in $_GET['sort'].

$_GET['sort'] is not set on the initial page load so is given the default value set in the admin, or if nothing is defined, there is a hard-coded '20a' in the file.

Clicking on the product listing column headers will set a new value for $_GET['sort'] which will be used subsequently and thus override the default sort order.

In Admin->Configuration->Product Listing there are options which enable the display of the columns available.

admin option: display product listing default sort order

What is not immediately apparent is that you can set the display order of those columns, eg. where Product Image is column 1, Product Name is column 2 and Product Price is column 3 as shown in the example.

The default sort order of the products in those columns is determined by Display Product Listing Sort Order.
eg.
2a means sort by column 2 (Display Product name in this example), descending: Z->A.
3d means sort by column 3 (Display Product Price/add to cart in this example), ascending: low price->high price.

This value is named PRODUCT_LISTING_DEFAULT_SORT_ORDER in the database/code.

So, when $_GET['sort'] is not defined on initial page load (or is invalid/has anything other than "1-8" or "a" or "d" in it) then $_GET['sort'] will be set to the column of the Product Name and is hard-coded as ascending.

So if you've set Product Name as column 7, $_GET['sort'] will be set to 7a.

If you have switched off "Display Product Name", a hard-coded default setting of 20a is used (line 117):

// if set to nothing use products_sort_order and PRODUCTS_LIST_NAME is off
if (PRODUCT_LISTING_DEFAULT_SORT_ORDER == '') {
$_GET['sort'] = '20a';

If something is defined in there, it is examined by the following code:

$sort_col = substr($_GET['sort'], 0 , 1);
$sort_order = substr($_GET['sort'], 1);
switch ($column_list[$sort_col-1]) {

where the column to use for sorting is extracted/identified, followed by the a or d for the sort_order.

So that's the default setting.

How the mod works

The drop-down adds another GET parameter to the urls that are used by the product-listing-name links and the return-to-listing button (in the prev-listing-next nav buttons) to preserve the listing sort order.

The sql for the sorted/filtered product listing is passed to a session variable for use by prev-next.

Version History

v2.0 2013-08-30 - torvista

Code completely rewritten to add to rather than replace existing filters and make use of Zen Cart 1.60 drop-down listing code.
Functionality expanded to maintain the selected filter and sort order through to the product info page/prev-next navigation and back to the listing.
Readme updated.

v1.2 2013-01-13 - torvista

Hard-coded constants removed: english and spanish defines added.
Refactored code to use overrides as much as possible.
Added code for Advanced Search Result
Added debugging.
Added new readme.
Repackaged as Product Listing Sorter instead of Product Filter Module...as its not a filter!

v1.1 2011-11-22 - Bente Mihai (bente.mihai@gmail.com) as Product Filter Module for Zen Cart

v1.0 2010-03-02 - sribala. Contus Support Interactiv (sic) as Product Filter Module for Zen Cart