Bug Fix for 2.2.1 from CS-Cart Help Desk

There is a bug in 2.2.1 that occurs when you try to modify any product. If you modify and save a product, CS-Cart will reset your number positioning of the product in its category etc to 0.



I worked with CS-Cart help desk to solve the problem and they said it was ok to post the fix, so thought I would share it. I can confirm the fix below solves the issue.





In order to fix this bug replace the following part of code:

fn_update_product($_REQUEST['product_data'], $_REQUEST['product_id'], DESCR_SL);
$_main_category = db_get_field("SELECT category_id FROM ?:products_categories WHERE product_id = ?i AND link_type = 'M'", $_REQUEST['product_id']);
$_add_categories = db_get_fields("SELECT category_id FROM ?:products_categories WHERE product_id = ?i", $_REQUEST['product_id']);
if (!empty($_REQUEST['product_data']['add_categories'])) {
$add_categories = explode(',', $_REQUEST['product_data']['add_categories']);
$main_category = (!empty($_REQUEST['product_data']['main_category'])) ? $_REQUEST['product_data']['main_category'] : $add_categories[0];
} else {
$add_categories = $_add_categories;
$main_category = $_main_category;
}

db_query(&quo!
t;DELETE FROM ?:products_categories WHERE product_id = ?i", $_REQUEST['product_id']);
fn_update_product_count($_add_categories);
$new_ids = $add_categories;
$_data = array (








with this one:

fn_update_product($_REQUEST['product_data'], $_REQUEST['product_id'], DESCR_SL);
$_main_category = db_get_row("SELECT category_id, position FROM ?:products_categories WHERE product_id = ?i AND link_type = 'M'", $_REQUEST['product_id']);
$_add_categories = db_get_array("SELECT category_id, position FROM ?:products_categories WHERE product_id = ?i ORDER BY category_id", $_REQUEST['product_id']);
$add_categories = Array();
foreach($_add_categories as $_category) {
$add_categories[] = $_category['category_id'];
$add_categories_positions[$_category['category_id']] = $_category['position'];
}
$main_category_position = $_main_category['position'];
if (!empty($_REQUEST['product_data']['add_categories'])) {
$add_categories = explode(',', $_REQUEST['product_data']['add_categories']);
$main_categor!
y = (!empty($_REQUEST['product_data']['main_category'])) ? $_REQUEST['product_data']['main_category'] : $add_categories[0];
} else {
$main_category = $_main_category['category_id'];
}
db_query("DELETE FROM ?:products_categories WHERE product_id = ?i", $_REQUEST['product_id']);
fn_update_product_count($add_categories);
$new_ids = $add_categories;
$_data = array (








and replace the following part of code:

foreach ($add_categories as $c_id) {
$_data['category_id'] = $c_id;
db_query("INSERT INTO ?:products_categories ?e", $_data);
}




with this one:

foreach ($add_categories as $c_id) {
$_data['category_id'] = $c_id;
if (isset($add_categories_positions[$c_id])) {
$_data['position'] = $add_categories_positions[$c_id];
} else {
$_data['position'] = 0;
}
db_query("INSERT INTO ?:products_categories ?e", $_data);
}








in the “products.php” file, located in the “controllers/admin” directory.

Could you add code tags


Makes things much cleaner


around your code areas?

[quote name=‘tbirnseth’ timestamp=‘1311027067’ post=‘117612’]

Could you add code tags

<br />
Makes things much cleaner<br />

```<br />
around your code areas?<br />
[/quote]<br />
Just for you Tony <img src="upload://yGrKuhdxbbf2nIFvV2XDJuEFQb7.gif" class="bbc_emoticon" alt=":)">

Thank you .

We also have the similar problem, but this FIX is not working for us. Isn't some error in it?

[quote name='MimiWatch' timestamp='1311179410' post='117784']

We also have the similar problem, but this FIX is not working for us. Isn't some error in it?

[/quote]



I got an error the first time I did it and found I had not properly copied the full code I was missing a word. When I redid it worked. Please double check I will also double check the code I posted but should work. You can also create a trouble ticket with CS-Cart they are aware of the problem and can probably log into your server and resolve it.

Hi,

here you can find the fix in the unified diff format: http://forum.cs-cart.com/tracker/issue-2630-position-order-reset-when-product-is-saved/



Please note that you should apply it to original files of version 2.2.1.