Monday, July 23, 2012

Virtuemart Pagination – Set products per page and Solution for Pagination Issue

To do this, 

Locate the file named “shop.browse.php” in “administrator/components /com_virtuemart/html/” directory. Then add the following code line next to the mm_showMyFileName( __FILE__ ); line in that file:
 
if (!isset($_GET['limit'])) { $limit = 2; }


This 2 is the number of products that needs to be per page. Change it as per your requirement.

Still there is a drop down in the page that the users can select the number of items that they need per page. 

You can either simply disable this or just locate the file named “pageNavigation.class.php” inside “administrator/components/com_virtuemart/classes/” directory and change the following for loop as per your requirement (usually the for loop is at somewhere around line 59):

 for ($i=5; $i <= 30; $i+=5) { 
if (empty( $link)) 
{        $limits[$i] = $i;      }
else {   $limits[vmRoute($link.'&limit='.$i)] = $i;      }
}

Example:

I need to show 2 items default and maximum items per page that the user can select is 12. 

The method is as follows:

function getLimitBox ( $link = ”) {
$limits = array();
if (!empty($link) && strpos( ‘limitstart=’, $link) === false) {
$link .= ‘&limitstart=’.$this->limitstart;
}
for ($i=2; $i <= 10; $i+=2) { //I want to show default 2 and there after select options 4,6,8,…,12.
if (empty( $link)) {
$limits[$i] = $i;
} else {
$limits[vmRoute($link.'&limit='.$i)] = $i;
}
}
if (empty( $link)) {
$limits[12] = 12; //Set the maximum value you need allow users to select
} else {
$limits[vmRoute($link.'&limit=50')] = 12; //set the maximum value you need to allow the users to select
}
// build the html select list
if (empty( $link)) {
$html = ps_html::selectList( ‘limit’, $this->limit, $limits, 1, ”,  ‘onchange=”this.form.submit();”‘ );
} else {
$current = vmRoute($link.’&limit=’.$this->limit);
$html = ps_html::selectList( ‘limit’, $current, $limits, 1, ”,  ‘onchange=”location.href=this.value”‘ );
}
$html .= “\n<input type=\”hidden\” name=\”limitstart\” value=\”$this->limitstart\” />”;
return $html;
}

Still if you have pagination enabled in your pages there is a common problem in Virtuemart that is the assignment of wrong ItemId in some cases. 

This makes pagination doesn’t work properly most of the times. 

To resolve this issue – locate the “ps_session.php” file in the “administrator/components/com_virtuemart/classes/” directory. 

Now add the following as the first line inside the “getShopItemid()” function:

if(!empty($_REQUEST['Itemid'])) { $_REQUEST['shopItemid'] = $_REQUEST['Itemid']; }


1 comment:

pawa said...

to which version of virtuemart does this fix apply?