Save order for manual product ordering in category does not work

Started by porscha, October 16, 2012, 12:27:17 PM

Previous topic - Next topic

BaidareW


Joseph Kwan

There are two more bugs:
1. In the file administrator/components/com_virtuemart/models/product.php, before this line (line 851 for 2.0.14) within the function getProductSingle:

$product->virtuemart_category_id = $product->categories[0];

insert these lines

// added by JK to fix bug
$virtuemart_category_id = JRequest::getInt ('virtuemart_category_id', 0);
if ($virtuemart_category_id)
$product->virtuemart_category_id = $virtuemart_category_id;
else
// added by JK ends

This will fix the cases when one product belongs to multiple categories.

2. In the file administrator/components/com_virtuemart/helpers/vmtablexarray.php, change this line (line 71 for version 2.0.14)

$query = 'SELECT `id` WHERE $this->_pkey = '.(int)$cid[0].' AND `virtuemart_category_id` = '.(int)$skeyId ;

to

$query = 'SELECT `id` FROM `' . $this->_tbl . '` WHERE ' . $this->_pkey . ' = '.(int)$cid[0].' AND `virtuemart_category_id` = '.(int)$skeyId ; //mod by JK to fix bug

This will fix the bug when using orderup and orderdown icon to change the sort order.

These bug fixes are funded by a client. It may not work in all cases. Please test it and report back.
Joomla/VM Upgrade Services. Problems with your migration? We can help.
Custom extensions to VM. Performance Tuning. Template modifications and advices.
Pay service to make VM work according to your needs. Your Joomla/VM solutions are just a PM away.

CE WebDesign München

Perfect, great, thank you and your client for sharing this!!!
Works nice in 2.0.14 (and 2.0.8c - line 689 for 1.bug)

would you be interrested in a job fixing 'odering of related products'?
http://forum.virtuemart.net/index.php?topic=108177.msg370553#msg370553
CE WebDesign München: https://ce-webdesign.de | Websites, eCommerce WebShops | Responsive Design | SEO

Joseph Kwan

Thanks gogo123. That request may not be straight forward. Please read the PM I sent.
Joomla/VM Upgrade Services. Problems with your migration? We can help.
Custom extensions to VM. Performance Tuning. Template modifications and advices.
Pay service to make VM work according to your needs. Your Joomla/VM solutions are just a PM away.

CE WebDesign München

Fixes still work, but when editing a product, on save
the order of products in its categories gets lost (j258+vm2014)
CE WebDesign München: https://ce-webdesign.de | Websites, eCommerce WebShops | Responsive Design | SEO

Joseph Kwan

Hmm.. this can be subtle. Not sure if this works but you can try. Replace fix #1 above with
1. In the file administrator/components/com_virtuemart/models/product.php, replace this line (line 851 for 2.0.14) within the function getProductSingle:

$product->virtuemart_category_id = $product->categories[0];

with these lines


// mod by JK to fix bug
$product->virtuemart_category_id = JRequest::getInt ('virtuemart_category_id', 0);
// mod by JK ends



I hope this will add an ordering for showing all products (without an association with any category). But probably some code elsewhere may need to modify as well.
Joomla/VM Upgrade Services. Problems with your migration? We can help.
Custom extensions to VM. Performance Tuning. Template modifications and advices.
Pay service to make VM work according to your needs. Your Joomla/VM solutions are just a PM away.

Joseph Kwan

This replacement code will work better.


$product->virtuemart_category_id = $this->virtuemart_category_id;
Joomla/VM Upgrade Services. Problems with your migration? We can help.
Custom extensions to VM. Performance Tuning. Template modifications and advices.
Pay service to make VM work according to your needs. Your Joomla/VM solutions are just a PM away.

CE WebDesign München

thanks for providing help, works perfect!

so with new fix #1 + old fix #2:

orderup and -down icon still working good (in-/decrease product ordernumber,
though not switching place with next/previous - but fine with me)

odering when product belongs to multiple categories still working good and per category

on saving product with multiple categories,
odering is not changed (checked both categories), so this is fixed!!!

so great, for me it's fixed, thank you so much!!!
CE WebDesign München: https://ce-webdesign.de | Websites, eCommerce WebShops | Responsive Design | SEO

sjshaffer

I had understood this should have been fixed in version 2.0.14 but it is still giving me trouble.  I may be asking too much in that I have 3 levels of category hierarchy and want to display products at all three levels.  I have given up trying to order the lower two levels and am manually ordering at the top level only. Typing in an order and then saving it does not seem to work correctly.  The order displayed from the admin view is not the same as the order number.  It might be that the sort for the admin view and possibly the customer view are treating the order as text.  The only way I have been able to get ordering to work (top level) is by usingthe up/down arrows (and they don't work as expected often jumping several positions).

CE WebDesign München

hi, if it is still giving you trouble, please backup your site, test the fixes mentioned above and post here, if it works for all 3 levels or test 2.0.15b (not for live site!) - version 2.0.14 works great for me with these fixes.
CE WebDesign München: https://ce-webdesign.de | Websites, eCommerce WebShops | Responsive Design | SEO

Milbo

Kwan
Please how do you think should be this part be written?

if (!empty($product->categories) and is_array ($product->categories) and !empty($product->categories[0])) {
$product->virtuemart_category_id = $product->categories[0];


Added, please test
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

hobnob

I tried this  fix and my child products are still arranged arbitrarily. I'm using the latest version of virtuemart and joomla. Everything is updated. Any ideas?
Thanks!

My site is here:
http://haydenmedicalinc.com/joomla/viretuemart/7/3/bariatric-instruments/forceps-bariatric/debakey-forceps-bariatric-detail

Quote from: porscha on October 16, 2012, 12:49:59 PM
I use Joomla 2.5.6 an VM 2.0.8e

this is from
administrator/components/com_virtuemart/models/product.php

/* reorder product in one category
    * TODO this not work perfect ! (Note by Patrick Kohl)
   */
   function saveorder ($cid = array(), $order, $filter = NULL) {

      JRequest::checkToken () or jexit ('Invalid Token');

      $virtuemart_category_id = JRequest::getInt ('virtuemart_category_id', 0);

      $q = 'SELECT `id`,`ordering` FROM `#__virtuemart_product_categories`
         WHERE virtuemart_category_id=' . (int)$virtuemart_category_id . '
         ORDER BY `ordering` ASC';
      $this->_db->setQuery ($q);
      $pkey_orders = $this->_db->loadObjectList ();

      $tableOrdering = array();
      foreach ($pkey_orders as $order) {
         $tableOrdering[$order->id] = $order->ordering;
      }
      // set and save new ordering
      foreach ($order as $key => $ord) {
         $tableOrdering[$key] = $ord;
      }
      asort ($tableOrdering);
      $i = 1;
      $ordered = 0;
      foreach ($tableOrdering as $key => $order) {
//          if ($order != $i) {
         $this->_db->setQuery ('UPDATE `#__virtuemart_product_categories`
               SET `ordering` = ' . $i . '
               WHERE `id` = ' . (int)$key . ' ');
         if (!$this->_db->query ()) {
            vmError ($this->_db->getErrorMsg ());
            return FALSE;
         }
         $ordered++;
//          }
         $i++;
      }
      if ($ordered) {
         $msg = JText::sprintf ('COM_VIRTUEMART_ITEMS_MOVED', $ordered);
      }
      else {
         $msg = JText::_ ('COM_VIRTUEMART_ITEMS_NOT_MOVED');
      }
      JFactory::getApplication ()->redirect ('index.php?option=com_virtuemart&view=product&virtuemart_category_id=' . $virtuemart_category_id, $msg);

   }

   /**
    * Moves the order of a record
    *
    * @param integer The increment to reorder by
    */
   function move ($direction, $filter = NULL) {

      JRequest::checkToken () or jexit ('Invalid Token');

      // Check for request forgeries
      $table = $this->getTable ('product_categories');
      $table->move ($direction);

      JFactory::getApplication ()->redirect ('index.php?option=com_virtuemart&view=product&virtuemart_category_id=' . JRequest::getInt ('virtuemart_category_id', 0));
   }

jingtao

I am using J2.5.8 VM2.0.18a . I can confirm that setting the ordering of products which belong to multiple categories still does not work correctly.

I am not able to apply any of the hacks suggested here as the administrator/components/com_virtuemart/modesl/product.php looks quite different from the previous versions mentioned here (2.0.14 and before).


jingtao

I am happy to say that I have found a fix for 2.0.18a

Roughly, here's how to reproduce the bug:
1. Assign products to category A and B
2. In products list view, filter by category B
3. Set ordering for products
4. The ordering is saved to category A instead

The problem is, if a product is assigned to several categories, when you set the ordering for that product in one particular category, Virtuemart will always load and save the ordering to the first category it belongs to.

And here's the fix:

In administrator/components/com_virtuemart/models/product.php , function getProductSingle , line 898

replace this :


} else {
$product->virtuemart_category_id = JRequest::getInt ('virtuemart_category_id', 0);


if (!empty($product->categories) and is_array ($product->categories) and !empty($product->categories[0])) {
$product->virtuemart_category_id = $product->categories[0];
} else {
$product->virtuemart_category_id = 0;
}

}


with this:


} else {
$product->virtuemart_category_id = $this->virtuemart_category_id; // shouldn't get from JRequest. This gets from the filtering state in the model object
if ( !empty($product->virtuemart_category_id) ) {
// don't do anything, we already have the right category id from JRequest
} else if (!empty($product->categories) and is_array ($product->categories) and !empty($product->categories[0])) {
$product->virtuemart_category_id = $product->categories[0];
} else {
$product->virtuemart_category_id = 0;
}
var_dump($product->virtuemart_category_id);
}


I have checked against 2.1 in the SVN trunk and this bug still exists there. Is there any chance this can be merged into 2.1, Max?

dorandis

Just wanted to say thanks to jingtao, the shop finally sports sorted shoesizes!
Dorandis (very happy)