Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impossible to change position of products into a category in the catalog > product page #14879

Merged
merged 6 commits into from Aug 28, 2019

Conversation

@PierreRambaud
Copy link
Contributor

commented Jul 26, 2019

Questions Answers
Branch? develop
Description? Use a different way to sort product in categories
Type? bug fix
Category? BO
BC breaks? no
Deprecations? no
Fixed ticket? Fixes #14616
How to test? Follow ticket instructions.

This change is Reviewable

@PierreRambaud PierreRambaud requested a review from PrestaShop/prestashop-core-developers as a code owner Jul 26, 2019
@PierreRambaud PierreRambaud changed the title [POC] Impossible to change position of products into a category in the catalog > product page Impossible to change position of products into a category in the catalog > product page Jul 29, 2019
@ABarailler

This comment has been minimized.

Copy link

commented Jul 29, 2019

Hello,
I have tested the commit e199ed4 on two projects and this is not working.
But I have tried to modify the SQL request and I found a way where the product's position updates well

	$firstPosition = min($positionsMatcher);
	$productsIds = implode(',', array_map('intval', array_keys($productList)));
            $updatePositions = 'UPDATE `' . _DB_PREFIX_ . 'category_product` cp
		INNER JOIN `' . _DB_PREFIX_ . 'product` p ON (cp.`id_product` = p.`id_product`)
		' . Shop::addSqlAssociation('product', 'p') . '
		INNER JOIN (
			SELECT id_product
			FROM `' . _DB_PREFIX_ . 'category_product` cp
			WHERE cp.`id_category` = ' . (int) $categoryId . ' AND cp.`id_product` IN (' . $productsIds . ')
			ORDER BY FIELD(cp.`id_product`, ' . $productsIds . ')
		)
		AS cp2 ON cp2.id_product = p.id_product
		SET cp.`position` = (SELECT @i := @i + 1),
			p.`date_upd` = "' . date('Y-m-d H:i:s') . '",
			product_shop.`date_upd` = "' . date('Y-m-d H:i:s') . '"
		WHERE cp.`id_category` = ' . (int) $categoryId . ' AND cp.`id_product` IN (' . $productsIds . ')';

	Db::getInstance()->query('SET @i := ' . (((int) $firstPosition) - 1));
	Db::getInstance()->query($updatePositions);`

It's not perfect but that works

@PierreRambaud

This comment has been minimized.

Copy link
Contributor Author

commented Jul 29, 2019

@ABarailler Can you tell me what was the error?
You do

SELECT id_product
FROM `' . _DB_PREFIX_ . 'category_product` cp
WHERE cp.`id_category` = ' . (int) $categoryId . ' AND cp.`id_product` IN (' . $productsIds . ')
ORDER BY FIELD(cp.`id_product`, ' . $productsIds . ')

Which is already done by the:

'INNER JOIN `' . _DB_PREFIX_ . 'product` p ON (cp.`id_product` = p.`id_product`) ' .
" . Shop::addSqlAssociation('product', 'p') . ' ' .
// AND
'ORDER BY FIELD(cp.`id_product`, ' . $productsIds . ')';

Same for

AS cp2 ON cp2.id_product = p.id_product

Which is

'INNER JOIN `' . _DB_PREFIX_ . 'product` p ON (cp.`id_product` = p.`id_product`) ' .
@ABarailler

This comment has been minimized.

Copy link

commented Jul 29, 2019

No problem @PierreRambaud,
The initial problem is on ORDER BY, on SQL we can't add ORDER BY on multi table UPDATE.
The only solution I found is to replace the "order by" by a "inner join" that works the same way

It's probably not the best solution, but I can't find a better way right now.

@PierreRambaud

This comment has been minimized.

Copy link
Contributor Author

commented Jul 29, 2019

@ABarailler Thanks for explanation. Your solution isn't working for me 😅
Are you able to test this one?
On request for postion, and one for multiple table (we don't care for order here).

@ABarailler

This comment has been minimized.

Copy link

commented Jul 29, 2019

OK on one project I found a case that my SQL doesn't work.
But I test your script on two projects and it works

Thanks for the fix @PierreRambaud

@PierreRambaud

This comment has been minimized.

Copy link
Contributor Author

commented Jul 29, 2019

@ABarailler Thanks to you for helping us to make this project better 😸

@PierreRambaud PierreRambaud force-pushed the PierreRambaud:fix/14616 branch from b190c33 to 63b0823 Jul 29, 2019
@matks matks added this to the 1.7.7.0 milestone Aug 20, 2019
@sarahdib sarahdib self-assigned this Aug 22, 2019
@sarahdib

This comment has been minimized.

@PierreRambaud PierreRambaud force-pushed the PierreRambaud:fix/14616 branch from 63b0823 to 58343dd Aug 28, 2019
@sarahdib

This comment has been minimized.

Copy link

commented Aug 28, 2019

@PierreRambaud The problem is fixed if we use the icon in position column. The product is selected and we can change the position and saved and all changes is validated.

Since this PR correct the problem it's OK a New issue was created for the second issue : #15291

@matks
matks approved these changes Aug 28, 2019
@PierreRambaud PierreRambaud merged commit cf08c72 into PrestaShop:develop Aug 28, 2019
2 checks passed
2 checks passed
PrettyCI Code formatting
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@PierreRambaud PierreRambaud deleted the PierreRambaud:fix/14616 branch Aug 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.