Jump to content

Change Category of Multiple Products


Recommended Posts

Possibly,
try installing this module
export all your products in to a csv file - amend the category column and then use the import tool, not sure if this would work, but dont see why not, let me know if you need help.

Link to comment
Share on other sites

  • 5 months later...

Would you mind sharing your sql query? I'm trying to get the right combination, but not having success. I only get some of the correct data over. Do you have a list of all the tables that need to be included for categories and products to get exported/imported correctly?

  • Like 1
Link to comment
Share on other sites

Sure. I found out the ID numbers of the old category, and the new category products were to be moved into. You can find this by hovering your mouse over the category hyperlink in Prestashop admin. Then run the following SQL in PHPMyAdmin:

 

update ps_category_product set id_category = replace(id_category, 'Old ID', 'New ID');

ps_category_product is a table which contains matching category IDs and products IDs.

  • Like 2
Link to comment
Share on other sites

  • 2 months later...

Sure. I found out the ID numbers of the old category, and the new category products were to be moved into. You can find this by hovering your mouse over the category hyperlink in Prestashop admin. Then run the following SQL in PHPMyAdmin:

 

 

ps_category_product is a table which contains matching category IDs and products IDs.

Thanks a lot man !!! u save a lot of work, and time. My import had a faulty row, and i ended up with some messed up category/products.

Link to comment
Share on other sites

  • 1 month later...

INSERT INTO ps_category_product

(id_category, id_product, position)

 

select 38, id_product, id_product

from ps_category_product as c

where

id_category in (30,31,32,33,34,35,36);

 

 

where 38 is category you want to add to a product

and (30,31,32,33,34,35,36, etc..) are the categories where actually products are!

  • Like 2
Link to comment
Share on other sites

  • 2 months later...
  • 2 years later...

INSERT INTO ps_category_product

(id_category, id_product, position)

 

select 38, id_product, id_product

from ps_category_product as c

where

id_category in (30,31,32,33,34,35,36);

 

 

where 38 is category you want to add to a product

and (30,31,32,33,34,35,36, etc..) are the categories where actually products are!

Thanks very much SteFer you made my day .. :)

Link to comment
Share on other sites

  • 5 years later...

Can you please let me know where we need to run update query in prestashop to change the category id. As I can see only select query is allowed in database section under advance parameter. I need to update category id of around 2000 products at a time. Please help me on this and let me know we can run multiple update queries at a time.

Link to comment
Share on other sites

  • 11 months later...

If you trying to move all products from one category to another, then you can try to use something like that

/* 
* author: Wiktor Liszkiewicz
* [email protected]
* test at: https://paiza.io/projects/Rn1YBOVx4JkDo7YHu2xVtg?language=mysql
* This scripts moves products from one category to another and also clenup the source category-product entry if the data is already asigned to destination category..
*/

-- create table as in PrestaShop (clone) from https://github.com/PrestaShop/PrestaShop/blob/1.7.7.x/install-dev/data/db_structure.sql
CREATE TABLE PREFIX_category_product (
  id_category int(10) unsigned NOT NULL,
  id_product int(10) unsigned NOT NULL,
  position int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (id_category, id_product),
  INDEX (id_product),
  INDEX (id_category, position)
);

-- insert demo data set I
INSERT INTO PREFIX_category_product (id_category, id_product, position)
VALUES
(0, 5416, 1),
(1, 5416, 1),
(2, 5416, 1),
(3, 5416, 1),
(0, 5418, 2),
(1, 5418, 2),
(2, 5418, 2),
(3, 5418, 2);

-- insert demo data set II
INSERT INTO PREFIX_category_product (id_category, id_product, position)
VALUES
(4, 2000, 1),
(5, 2000, 1),
(4, 5000, 2),
(5, 5000, 2);

-- check table data
SELECT * FROM PREFIX_category_product;

-- enter products id manualy or fint them wi query below
-- define category
SET @OLDCAT := 4;
SET @NEWCAT := 0;

-- set @STARTINGPOS position starting number will be 0 + 1 
SET @STARTINGPOS := 0;
-- or set @STARTINGPOS to the highest position value + 1 if category already have products 
select @STARTINGPOS := MAX(position) from PREFIX_category_product where id_category = @NEWCAT;
-- select @STARTINGPOS; -- debug

START TRANSACTION; -- start transation

-- update category where id_product is in old category and is not in new category (cleen up later with DELETE query)
UPDATE PREFIX_category_product 
    SET id_category = @NEWCAT,
    position = (select @STARTINGPOS := @STARTINGPOS + 1)
    WHERE id_category = @OLDCAT 
    AND id_product IN 
    (SELECT * FROM (SELECT DISTINCT id_product FROM PREFIX_category_product WHERE id_category = @OLDCAT) AS t1)
    and id_product NOT IN 
    (SELECT * FROM (SELECT DISTINCT  id_product FROM PREFIX_category_product WHERE id_category = @NEWCAT) AS t2);

-- remove products and category set if not moved because they are already assigned to destination category
DELETE FROM PREFIX_category_product WHERE id_product IN
    (SELECT * FROM (SELECT DISTINCT id_product FROM PREFIX_category_product WHERE id_category = @OLDCAT) AS t3)
    AND id_category = @OLDCAT;
    

COMMIT; -- end transation
    
SELECT * FROM PREFIX_category_product;

This would output the fallowing: 
image.png.a8739dac28346705b3b02b415a87f9b5.png

The only thing you care about:
image.png.77bb65feaedbb40b9d7b1ed4d833ee26.png

Also make sure to change table prefi:

image.png.a37b6c43748253c451e99c9f9ce8f58e.png

to the one that you actually use (most likely it will be "ps_").

If you interested in running query on production then be sure to make a backup before attempting to do anything. FInd out your category sorce and destination ID and replace all "PREFIX_" in the query below with your own prefix (usually "ps_"). If you database is safe and you make everything ready then you can run the SQL query below. You could use phpMyAdmin or MySQL Workbench.

SET @OLDCAT := 4;
SET @NEWCAT := 0;

-- set @STARTINGPOS position starting number will be 0 + 1 
SET @STARTINGPOS := 0;
-- or set @STARTINGPOS to the highest position value + 1 if category already have products 
select @STARTINGPOS := MAX(position) from PREFIX_category_product where id_category = @NEWCAT;
-- select @STARTINGPOS; -- debug

START TRANSACTION; -- start transation

-- update category where id_product is in old category and is not in new category (cleen up later with DELETE query)
UPDATE PREFIX_category_product 
    SET id_category = @NEWCAT,
    position = (select @STARTINGPOS := @STARTINGPOS + 1)
    WHERE id_category = @OLDCAT 
    AND id_product IN 
    (SELECT * FROM (SELECT DISTINCT id_product FROM PREFIX_category_product WHERE id_category = @OLDCAT) AS t1)
    and id_product NOT IN 
    (SELECT * FROM (SELECT DISTINCT  id_product FROM PREFIX_category_product WHERE id_category = @NEWCAT) AS t2);

-- remove products and category set if already moved or exsists in new category table
DELETE FROM PREFIX_category_product WHERE id_product IN
    (SELECT * FROM (SELECT DISTINCT id_product FROM PREFIX_category_product WHERE id_category = @OLDCAT) AS t3)
    AND id_category = @OLDCAT;
    

COMMIT; -- end transation

 

 

Edited by DevWL
smal fix (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...