News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Product List Sorting in Category VM 2.0.22a

Started by optmax, July 28, 2013, 14:15:23 PM

Previous topic - Next topic

optmax

VM 2.0.22a
Please jump to the later post
http://forum.virtuemart.net/index.php?topic=117523.msg399701#msg399701
for my final solution
As I reported in the parent board I discovered two problems in in the OrderBy list generated by administrator/components/com_virtuemart/models/product.php

Problem 1
After changing the OrderBy field, re-selecting the default OrderBy is ignored - the previously selected ordering is used.

Suggested solution
Change
145     $filter_order_raw = $this->getLastProductOrdering($this->_selectedOrdering);
to
145     $filter_order_raw = VmConfig::get ('browse_orderby_field');  //use the configured default orderby field


Problem 2
Appending an OrderBy clause to the URL of the Category Product List creates a duplicate of the ordering field in the OrderBy list
(For example you could do this in the default.php of com_virtuemart/categories so that you have different initial sort orders for particular categories.)

Suggested solution
Change
2121    foreach ($fields as $field) {
2122 if ($field != $orderby) {
2123

to
2121    foreach ($fields as $field) {
2122 if (stripos($field,$orderby)>0) {continue;} //if new orderby field already in list don't do anything
2123 if ($field != $orderby) {

Milbo

#1
Hmm we should first understand why getLastProductOrdering is not giving back the updated ordering.

We trigger in the product model updateRequests, which sets the ordering in the session, which is then given back. This is important to keep the ordering when you browse through your pagination. Your fix would destroy this. You may use vmdebug and check if updateRequests is fired or not.
Should I fix your bug, please support the VirtueMart project and become a [url=http://extensions.virtuemart.net/support/virtuemart-supporter-membership-detail]member[/url]
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

optmax

Thanks for that Milbo.

With your pointer I have looked at this again.
Quote from: Milbo on August 02, 2013, 11:24:38 AM
Hmm we should first understand why getLastProductOrdering is not giving back the updated ordering.
When the 'browse_orderby_field' (as configured in the BE) is re-selected on the FE category view, the 'orderby' for the SQL query is correctly set to 'orderbycfg' but the session 'vmlastproductordering' is not changed.

So at line 145 in the product model, $filter_order is being set to the previous orderby and not the re-selected default orderby.

So I think my suggested change at line 145 is needed.
AND I think  session 'vmlastproductordering' should be set to  'browse_orderby_field' .

This can be accomplished by taking the call to  'setLastProductOrdering' out of the else clause.

Change product.php

144 if($filter_order == "0"){
145 $filter_order_raw = $this->getLastProductOrdering($this->_selectedOrdering);
146 $filter_order = $this->checkFilterOrder ($filter_order_raw);
147 } else {
148 $filter_order = $this->checkFilterOrder ($filter_order);
149 $this->setLastProductOrdering($filter_order);
150
151 }

To

144 if($filter_order == "0"){
145 $filter_order_raw = VmConfig::get ('browse_orderby_field');  //use the configured default orderby field
146 $filter_order = $this->checkFilterOrder ($filter_order_raw);
147 } else {
148 $filter_order = $this->checkFilterOrder ($filter_order);
149
150 }
151 $this->setLastProductOrdering($filter_order);


Hopefully this will then not upset the pagination ordering.

(Alternatively I guess the 'setLastProductOrdering' could be called in getOrderByList rather than in updateRequests.)

optmax

#3
Milbo said
Quote from: Milbo on August 02, 2013, 11:24:38 AM
Hmm we should first understand why getLastProductOrdering is not giving back the updated ordering.
and this has prompted me to look at the problem afresh.

Problem Summary:
In Category view if the sort field is changed from the default and then back again the choice of the default sort  is ignored.

Cause:
In the ProductModel
  function updateRequests around line 142
$filter_order = JRequest::getString ('orderby', "0");
gets the filter_order from the URL orderby parameter.

But
  function getOrderByList
which prepares the links for the orderbylist does not append an orderby if the default order has been selected.

Solution:
Always append the orderby to the orderbylist links.

Code changes:
administrator/components/com_virtuemart/models/product.php
Around line 149 in function updateRequests remove or comment out $this->setLastProductOrdering($filter_order);
   // $this->setLastProductOrdering($filter_order);

In function getOrderByList around line 2056
Change
if ($orderby != '' && $orderby != $orderbyCfg) {
$orderbyTxt = '&orderby=' . $orderby;
}

To
// if ($orderby != '' && $orderby != $orderbyCfg) {
$orderbyTxt = '&orderby=' . $orderby;  //always add an orderby
// }
$this->setLastProductOrdering($orderby);

ie comment out the condition and insert the call to setLastProductOrdering

In function getOrderByList around line 2137
Change
if ($field == $orderbyCfg) {
$link = JRoute::_ ($fieldLink . $manufacturerTxt,FALSE);
}
else {
$link = JRoute::_ ($fieldLink . $manufacturerTxt . '&orderby=' . $field,FALSE);
}

to
// if ($field == $orderbyCfg) {
// $link = JRoute::_ ($fieldLink . $manufacturerTxt,FALSE);
// }
// else {
$link = JRoute::_ ($fieldLink . $manufacturerTxt . '&orderby=' . $fieldWithoutPrefix,FALSE);  //always add an orderby
// }


EDIT typo mentioned by Kok corrected
ie comment out all but one line, change the argument from $field to $fieldWithoutPrefix.
The change of the argument in the JRoute prevents problems with the Joomla SEF.
This should not cause other problems because function updateRequests makes calls to function checkFilterOrder which will, I think, append any table prefixes needed by the SQL so we don't need them in the link URL.

And one more change in  function getOrderByList around line 2137
Above
if ($field != $orderby) {
Insert
if (stripos($field,$orderby)>0) {continue;}
this is needed in case the default orderby has been appended to the URL in a template override, and prevents duplicate entries (of the default orderby field language value and its language constant)  in the orderbylist.

kok

Thank you, it works!
A small error - instead fieldfieldWithoutPrefix, to fieldWithoutPrefix
Joomla! 2.5.27
VirtueMart 2.6.10

elafynoh

Hi,

i changed the code in VM 2.0.24 from the last post, which solved the problem partially. It works if the default ordering in the VM setup is "Ascending" but it does not work if the default ordering is "Descending". I need this function, since the products should be listed from the newest to the oldest first.

best, Jörg

Milbo

No it should directly work in 2.0.24b.

his solution may work, but it is not following our pattern, the reason I could not directly use it.
Should I fix your bug, please support the VirtueMart project and become a [url=http://extensions.virtuemart.net/support/virtuemart-supporter-membership-detail]member[/url]
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/