Productdetail navigation with settings from configuration

Started by Peter Pillen, November 08, 2013, 02:09:45 AM

Previous topic - Next topic

Peter Pillen

Problem
The productnavigation in productdetail view does not follow the sort settings set in the virtuemart configuration. It only uses the product slug and ignores ASC or DESC set by the shop admin. I tested the code below just a few times and it seems to do the trick. Now if you choose to sort the products by any field and order as admin or visitor, the productdetailnavigation is also set to the same preferences. The product order of the productdetail navigation is now the same as in the category view.

In the file administrator\components\com_virtuemart\models\product.php replace the function public function getNeighborProducts to this below.

<?php
public function 
getNeighborProducts ($product$onlyPublished TRUE$max 1) {

$db JFactory::getDBO ();
$direction $this->filter_order_Dir;
$app JFactory::getApplication();
if ($app->isSite ()) {
$usermodel VmModel::getModel ('user');
$currentVMuser $usermodel->getUser ();
$virtuemart_shoppergroup_ids = (array)$currentVMuser->shopper_groups;
}

if(!empty($this->orderByString)){
$orderBy $this->orderByString;

} else {
$orderBy ' ORDER BY '.$this->filter_order.' ';
}

$joinPrice strpos($orderBy,'product_price');
$joinCat strpos($orderBy,'category_name');

$q 'SELECT `l`.`virtuemart_product_id`, `l`.`product_name`
FROM `#__virtuemart_products` as `p`
JOIN `#__virtuemart_products_' 
VMLANG '` as `l` using (`virtuemart_product_id`)
JOIN `#__virtuemart_product_categories` as `pc` using (`virtuemart_product_id`)'
;
if ($app->isSite ()) {
$q .= ' LEFT JOIN `#__virtuemart_product_shoppergroups` as `psgr` on (`psgr`.`virtuemart_product_id`=`l`.`virtuemart_product_id`)';
}

if ($joinPrice) {
$q .= ' LEFT JOIN `#__virtuemart_product_prices` as pp ON p.`virtuemart_product_id` = pp.`virtuemart_product_id` ';
}

if ($joinCat) {
$q .= ' LEFT JOIN `#__virtuemart_categories_' VMLANG '` as `c` using (`virtuemart_category_id`) ';
}

$q .= ' WHERE `virtuemart_category_id` = ' . (int)$product->virtuemart_category_id;

if ($app->isSite ()) {
if (is_array ($virtuemart_shoppergroup_ids)) {
$sgrgroups = array();
foreach ($virtuemart_shoppergroup_ids as $key => $virtuemart_shoppergroup_id) {
$sgrgroups[] = 'psgr.`virtuemart_shoppergroup_id`= "' . (int)$virtuemart_shoppergroup_id '" ';
}
$sgrgroups[] = 'psgr.`virtuemart_shoppergroup_id` IS NULL ';
$q .= " AND ( " implode (' OR '$sgrgroups) . " ) ";
}
}

if ($onlyPublished) {
$q .= ' AND p.`published`= 1';
}
$q .=  $orderBy $direction;

$db->setQuery ($q);
if ($result $db->loadAssocList ()) {
$key array_search(array('virtuemart_product_id' => $product->virtuemart_product_id'product_name' => $product->product_name) , $result);
$neighbors = array('previous' => array($result[(int)$key-1]), 'next' => array($result[(int)$key+1]));
}

$err $db->getErrorMsg();
if($err){
vmError('getNeighborProducts '.$err,'getNeighborProducts error');
}

return $neighbors;
}
?>


I left out the slug and ASC/DESC usage ... it now creates an array of all the products in that product's category, sorted according to the settings in the backend config (or set by visitor) and gets the key of the product your currently viewing ... automatically because the list is sorted, the key-1 is the previous record and the key+1 is the next record.

Only one problem left... when choosing a product from viewing the toplevel category, you always go to the sublevel category wherein that product is located. Therefore the productdetailnavigation is limited to that sublevel category.

Milbo

Did you test this with vm2.0.24a (the svn?) I think I fixed it there already
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Peter Pillen

I did not test the SVN version, but looked up the file (not even sure if I found the correct file) currently I'm looking at a product.php file with rev 7317 (location https://dev.virtuemart.net/svn/virtuemart/trunk/virtuemart/administrator/components/com_virtuemart/models)

The function getNeighborProducts still uses a hardcoded selection for ASC and DESC and has the slug as condition for sorting. No need to test it, because it is the same code I had in my current version and i'm 99% sure it's wrong.

unless ...

the neighbored products are now fetched in a different way and the function getNeighborProducts is overruled by some other piece of code elsewhere that has changed in a new version

or ...

I'm again looking at the wrong file (SVN tortoise is new to me ... like 50 minutes or so)

Robert_ITMan

P2 Peter - well done! This does correct the Productdetail navigation with settings from configuration

I confirmed that VirtueMart 2.0.24b still does not correct this. I then replaced the code with your revised 'public function getNeighborProducts' and I find that it works flawlessly - I further tested changing backend config to sort by Product Name both ASC and DESC and also by Product SKU both ASC and DESC and it works exactly as I would expect it to - that solves my issue, let me know if you need me to test it further.

Sweet Code! Hope to see this added in next update.

Hi also Milbo & VirtueMart Team - I am so close to finishing my first VM2 website and it ROCKS - I have everything working without a single hack to your program I have heavily customized the views in my template and added some nice plugins and I am VERY grateful for all the work that has gone into making VM 2 so much more than VM1... as soon as I launch this new site I will share it and add a more detailed review to Joomla.
WEBSITES @ OURFINGERTIPS
manage > develop > market > repeat

Save a lot of time and money when focused on building a website that works with marketing efforts to get more leads and sales from visitors.

www.ourfingertips.com

strandkrab

Just updated VM and the issue was not solved in the upgrade. So many thanks to P2 Peter for his code.

C-Box

Just upgraded today from 2.0.24 to 2.024b... and also got the getNeighborProducts Error.... thanks to P2Peter for the fix with the inital code of this thread..

It seems that the 2.0.24b has many errors concerning the ordering... so not a nice version. (had to fix the category.php at line 121 and also 130 concering a not initialized this$ reference)
Got this error in apache log files after my whole site was blank:

QuotePHP Fatal error:  Using $this when not in object context in .../www/cms/administrator/components/com_virtuemart/models/category.php on line 121

:-(

regards
C-Box

Milbo

c-box the ordering works in 2.0.24b. Please read the latest update information provided in the update view of your vm and please read here

http://forum.virtuemart.net/index.php?topic=120284.msg410104#msg410104

your error has nothing todo with Petes "fix"
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

C-Box

Hi Milbo

I know that Peters Fix hasn't to do anything with the ordering bug at category.php line 121 .. but it's also something with Ordering as he describes the ASC and DESC problems with sorting and I got these two errors after updating at 2.0.24b

I figured out that it seems the Phoca_VM module (http://www.phoca.cz/download/category/64-phoca-virtuemart-category-dtree-module#) that causes the error... unfortunately it's not just search & replace as you described as the phoca_VM_module has some other variables used and the syntax is a bit different...
D:\Chris\tmp\ex\modules\mod_phoca_vm_catdtree\mod_phoca_vm_catdtree.php (2 hits)
Line 39: $categories = $cache->call( array( 'VirtueMartModelCategory', 'getChildCategoryList' ),$vendorId, $category_id );
Line 59: $childCats = $cache->call( array( 'VirtueMartModelCategory', 'getChildCategoryList' ),$vendorId, $c->virtuemart_category_id );


perhaps someone will find a solution or even Phoca_VM will release an update...

regards
C-Box

Milbo

I think phoca will do an update. But it is exactly the same, as the colors in the other post describe it
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Peter Pillen

I am under the impression that this is solved in 2.0.26! Good work Milbo!