Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Let Customer choose number of products displayed on page

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 03-29-2008, 06:13 PM
 
wayfarer wayfarer is offline
 

Advanced Member
  
Join Date: Apr 2003
Posts: 56
 

Default Let Customer choose number of products displayed on page

I have had a long look through existing messages and haven't come across anyone who's done this before or asked about how to do it.

I was hoping to include a drop down menu, added to the 'Sort by' options across the top of a page of products, allowing the customer to select the number of products the page is showing. This is currently an admin setting.

How could I go about keeping the admin setting as the default, but allowing the user to choose to display a higher or lower number of products on the page, depending on the way they like to view products and the speed of their connection?

For example:

Products per page:
10
20
30
50
75
100
All

I guess the other consideration would be would how the option would work if the user is not logged in and how it would work for logged in users. For users not logged in, the preference lasts for the duration of the session. If they are logged in, perhaps that preference is retained... or maybe that would be too complicated...

Anyone with any ideas about what template changes to make to implement this?
__________________
X Cart Pro
v. 4.1.9
Download Expander (from Altered Cart)
X-Magnifier
Dynamic Preview (from BCS Engineering)
Customer Rewards Points (BCSE), used as credit purchasing system
Reply With Quote
  #2  
Old 03-30-2008, 03:58 AM
  Holub's Avatar 
Holub Holub is offline
 

X-Adept
  
Join Date: Jan 2008
Posts: 432
 

Default Re: Let Customer choose number of products displayed on page

Yes, you can do it.

First of all you should create file in root dir called "change_ppp.php" with this code:

File "change_ppp.php":
Code:
<?php if ( !defined('XCART_SESSION_START') && ($REQUEST_METHOD != "POST")) { header("Location: home.php");} require_once "auth.php"; x_session_register("products_per_page"); if ($REQUEST_METHOD=="POST") if ($ppp) { $back = $_SERVER['HTTP_REFERER']; if (!$back) $back = "/home.php"; if ($ppp == "all") $ppp = 32767; $products_per_page = $ppp; x_session_save("products_per_page"); func_header_location($back); } if ($products_per_page) { $config["Appearance"]["products_per_page"] = $products_per_page; $objects_per_page = $config["Appearance"]["products_per_page"]; } $smarty -> assign("products_per_page",$config["Appearance"]["products_per_page"]); ?>

Next step:

include into "auth.php" before end of file (?>)

Code:
include "change_ppp.php";

Next step:

include into /skin1/customer/main/products.tpl

Code:
<form method="POST" action="/change_ppp.php" name="product_per_page_form"> Show products per page <select name="ppp" onchange="javascript: document.product_per_page_form.submit();"> <option value="5" {if $products_per_page eq 5}selected{/if}>5</option> <option value="10" {if $products_per_page eq 10}selected{/if}>10</option> <option value="20" {if $products_per_page eq 20}selected{/if}>20</option> <option value="30" {if $products_per_page eq 30}selected{/if}>30</option> <option value="50" {if $products_per_page eq 50}selected{/if}>50</option> <option value="75" {if $products_per_page eq 75}selected{/if}>75</option> <option value="100" {if $products_per_page eq 100}selected{/if}>100</option> <option value="all" {if $products_per_page eq 32767}selected{/if}>all</option> </select> </form>

after

Code:
{* $Id: products.tpl,v 1.72.2.3 2006/11/27 11:40:25 max Exp $ *} {if $active_modules.Feature_Comparison ne '' && $products && $printable ne 'Y' && $products_has_fclasses} {include file="modules/Feature_Comparison/compare_selected_button.tpl"} {include file="modules/Feature_Comparison/products_check_js.tpl"} {/if}

This modification allows customer to select products per page and selected value will stored in session variable.

You can change this code as you want. I've checked this code and sure he works correctly.
__________________
Regards,
Anthony Holub

X-Cart Skins Store
- twenty two different skins available;
- both 4.1.x and 4.2.x versions compatible;
- refresh you store now!

Smart menu X-Cart add-on
Featured Products Slide Show X-Cart add-on
"What's New?" FREE X-Cart add-on
Reply With Quote

The following 2 users thank Holub for this useful post:
clik (04-24-2009), presson83 (11-10-2009)
  #3  
Old 03-30-2008, 04:05 AM
 
wayfarer wayfarer is offline
 

Advanced Member
  
Join Date: Apr 2003
Posts: 56
 

Default Re: Let Customer choose number of products displayed on page

Many thanks for the prompt reply Holub!... It's 1am here and I'm probably better to implement with a clear mind on a good night's sleep - looking forward to trying it first thing in the morning... thanks again.
__________________
X Cart Pro
v. 4.1.9
Download Expander (from Altered Cart)
X-Magnifier
Dynamic Preview (from BCS Engineering)
Customer Rewards Points (BCSE), used as credit purchasing system
Reply With Quote
  #4  
Old 03-30-2008, 04:08 AM
  Holub's Avatar 
Holub Holub is offline
 

X-Adept
  
Join Date: Jan 2008
Posts: 432
 

Default Re: Let Customer choose number of products displayed on page

If you have any questions or problems then feel free to ask any questions or information. I'm glad to help you.
__________________
Regards,
Anthony Holub

X-Cart Skins Store
- twenty two different skins available;
- both 4.1.x and 4.2.x versions compatible;
- refresh you store now!

Smart menu X-Cart add-on
Featured Products Slide Show X-Cart add-on
"What's New?" FREE X-Cart add-on
Reply With Quote
  #5  
Old 03-30-2008, 04:11 AM
 
wayfarer wayfarer is offline
 

Advanced Member
  
Join Date: Apr 2003
Posts: 56
 

Default Re: Let Customer choose number of products displayed on page

Will do, thanks and goodnight
__________________
X Cart Pro
v. 4.1.9
Download Expander (from Altered Cart)
X-Magnifier
Dynamic Preview (from BCS Engineering)
Customer Rewards Points (BCSE), used as credit purchasing system
Reply With Quote
  #6  
Old 03-30-2008, 01:46 PM
 
wayfarer wayfarer is offline
 

Advanced Member
  
Join Date: Apr 2003
Posts: 56
 

Default Re: Let Customer choose number of products displayed on page

It's a beautiful thing... Many thanks Holub.

I ended up placing it in /main/search_sort_by.tpl as I just wanted it to apply alongside the other sort by features, rather than having it on the featured products on the home page as well. I applied a little bit of styling and it's perfect.

For anyone else wanting to do the same, here's my /main/search_sort_by.tpl:

Code:
{* $Id: search_sort_by.tpl,v 1.5.2.2 2007/03/01 10:42:12 max Exp $ *} {if $url eq '' && $navigation_script ne ''}{assign var="url" value=$navigation_script|replace:"&":"&amp;"|cat:"&amp;"}{elseif $url ne ''}{assign var="url" value=$url|amp}{/if} <table cellspacing="0" cellpadding="0"> <tr> <td class="SearchSortTitle">{$lng.lbl_sort_by}:</td> {foreach from=$sort_fields key=name item=field} {if $navigation_page > 1} {assign var="cur_url" value=$url|cat:"page="|cat:$navigation_page|cat:"&amp;sort="|cat:$name|cat:"&amp;sort_direction="} {else} {assign var="cur_url" value=$url|cat:"sort="|cat:$name|cat:"&amp;sort_direction="} {/if} {if $name eq $selected} <td><a class="SearchSortLink" href="{$cur_url}{if $direction eq 1}0{else}1{/if}" title="{$lng.lbl_sort_by|escape}: {$field}"><img src="{$ImagesDir}/{if $direction}darrow.gif{else}uarrow.gif{/if}" class="SearchSortImg" alt="{$lng.lbl_sort_direction|escape}" /></a></td> {/if} <td class="SearchSortCell"><a class="SearchSortLink" href="{$cur_url}{if $name eq $selected}{if $direction eq 1}0{else}1{/if}{else}{$direction}{/if}" title="{$lng.lbl_sort_by|escape}: {$field}">{if $name eq $selected}<b>{/if}{$field}{if $name eq $selected}</b>{/if}</a></td> {/foreach} <td>:&nbsp;&nbsp;&nbsp;&nbsp;</td><td class="DropDownTitle"><form method="POST" action="/myshopdirectory/change_ppp.php" name="product_per_page_form"> <b>Show:</b> <select class="ShowDropDown" name="ppp" onchange="javascript: document.product_per_page_form.submit();"> <option value="10" {if $products_per_page eq 10}selected{/if}>10</option> <option value="20" {if $products_per_page eq 20}selected{/if}>20</option> <option value="30" {if $products_per_page eq 30}selected{/if}>30</option> <option value="50" {if $products_per_page eq 50}selected{/if}>50</option> <option value="75" {if $products_per_page eq 75}selected{/if}>75</option> <option value="100" {if $products_per_page eq 100}selected{/if}>100</option> <option value="all" {if $products_per_page eq 32767}selected{/if}>ALL</option> </select> </form></td></tr> </table>

Oh and for others wanting to implement Holub's solution here, you may have to resolve the path that points to change_ppp.php if your shop is in a subdirectory of your main domain name, as I did...

For Example: action="/yourshopdirectory/change_ppp.php"
__________________
X Cart Pro
v. 4.1.9
Download Expander (from Altered Cart)
X-Magnifier
Dynamic Preview (from BCS Engineering)
Customer Rewards Points (BCSE), used as credit purchasing system
Reply With Quote
  #7  
Old 03-30-2008, 09:32 PM
  Jon's Avatar 
Jon Jon is offline
 

X-Guru
  
Join Date: Oct 2002
Location: Vancouver, Canada
Posts: 4,200
 

Default Re: Let Customer choose number of products displayed on page

To err on the side of caution in preventing sql injection, I would modify change_ppp.php and:

FIND:
Code:
if ($ppp) {

CHANGE TO:
Code:
if ((int)$ppp > 0) {

FIND:
Code:
if ((int)$products_per_page > 0) {
Reply With Quote
  #8  
Old 03-31-2008, 12:56 AM
  Holub's Avatar 
Holub Holub is offline
 

X-Adept
  
Join Date: Jan 2008
Posts: 432
 

Default Re: Let Customer choose number of products displayed on page

In version 4.1.9 (as minimum) the auth.php file included in change_ppp.php will protect any POST variables from sql injection. However, God helps those who help themselves

Use for pleasure!
__________________
Regards,
Anthony Holub

X-Cart Skins Store
- twenty two different skins available;
- both 4.1.x and 4.2.x versions compatible;
- refresh you store now!

Smart menu X-Cart add-on
Featured Products Slide Show X-Cart add-on
"What's New?" FREE X-Cart add-on
Reply With Quote
  #9  
Old 03-31-2008, 01:26 AM
 
wayfarer wayfarer is offline
 

Advanced Member
  
Join Date: Apr 2003
Posts: 56
 

Thumbs up Re: Let Customer choose number of products displayed on page

Thanks to you both - I've implemented Jon's suggestion anyway and done some reading up on SQL injection.
__________________
X Cart Pro
v. 4.1.9
Download Expander (from Altered Cart)
X-Magnifier
Dynamic Preview (from BCS Engineering)
Customer Rewards Points (BCSE), used as credit purchasing system
Reply With Quote
  #10  
Old 03-31-2008, 07:42 AM
  Jon's Avatar 
Jon Jon is offline
 

X-Guru
  
Join Date: Oct 2002
Location: Vancouver, Canada
Posts: 4,200
 

Default Re: Let Customer choose number of products displayed on page

EDIT: $products_per_page however could possibly be specified from an external post.

I didn't go through the x-cart code to see if the query checks this variable prior to use or assumes it is safe because it usually comes from the config, but I figure better safe than sorry
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 06:33 AM.

   

 
X-Cart forums © 2001-2020