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

Allow changing the position of products in a category in the catalog > product page #14879

Merged
merged 6 commits into from Aug 28, 2019
Merged

Conversation

PierreRambaud
Copy link
Contributor

@PierreRambaud PierreRambaud 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 a team as a code owner July 26, 2019 16:53
@prestonBot prestonBot added develop Branch Bug Type: Bug labels 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
Copy link

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
Copy link
Contributor Author

PierreRambaud 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
Copy link

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
Copy link
Contributor Author

@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
Copy link

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
Copy link
Contributor Author

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

@matks matks added the Waiting for QA Status: action required, waiting for test feedback label Aug 20, 2019
matks
matks previously approved these changes Aug 20, 2019
@matks matks added this to the 1.7.7.0 milestone Aug 20, 2019
@sarahdib sarahdib self-assigned this Aug 22, 2019
@sarahdib
Copy link
Contributor

@sarahdib sarahdib added Waiting for author Status: action required, waiting for author feedback and removed Waiting for QA Status: action required, waiting for test feedback labels Aug 27, 2019
@sarahdib
Copy link
Contributor

sarahdib 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

@sarahdib sarahdib added QA ✔️ Status: check done, code approved and removed Waiting for author Status: action required, waiting for author feedback labels Aug 28, 2019
@PierreRambaud PierreRambaud merged commit cf08c72 into PrestaShop:develop Aug 28, 2019
@marlon8511
Copy link

Hello
I still have the issue. The order by position in the parent category of the BO doesn't work in FO when "Show products from subcategories" is activated

I use the AdminProductDataUpdater.php file found here and doesn't work for me

PS:1.7.6.3
Faceted Search: 3.4.1

@marionf
Copy link
Contributor

marionf commented Feb 10, 2020

@marlon8511
As you can see with the milestone, this issue has been fixed for the 1.7.7, so it's normal if it's not fixed in 1.7.6.3

Furthermore, there is still an issue about this not fixed: #14616 (comment)
#15291

@eternoendless eternoendless changed the title Impossible to change position of products into a category in the catalog > product page Allow changing the position of products in a category in the catalog > product page Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Type: Bug develop Branch QA ✔️ Status: check done, code approved
Projects
None yet
7 participants