X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Let Customer choose number of products displayed on page (https://forum.x-cart.com/showthread.php?t=38695)

wayfarer 03-29-2008 06:13 PM

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?

Holub 03-30-2008 03:58 AM

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.

wayfarer 03-30-2008 04:05 AM

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.

Holub 03-30-2008 04:08 AM

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.

wayfarer 03-30-2008 04:11 AM

Re: Let Customer choose number of products displayed on page
 
Will do, thanks and goodnight :-)

wayfarer 03-30-2008 01:46 PM

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"

Jon 03-30-2008 09:32 PM

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) {

Holub 03-31-2008 12:56 AM

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!

wayfarer 03-31-2008 01:26 AM

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.

Jon 03-31-2008 07:42 AM

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 :)


All times are GMT -8. The time now is 10:17 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.