Add all products from one category into another in Magento

IMAGE: Blueclaw

Today a client was restructuring their product catalog and needed to add all the products from one category into another as they were consolidating a few categories together. Well the below MySQL query did the job, so thought I would share in the event anybody else was looking to do the same thing.

INSERT INTO catalog_category_product
SELECT {{NEWCATEGORY}} as category_id, tablea.product_id, 999 as position
FROM `catalog_category_product`  tablea
LEFT OUTER JOIN `catalog_category_product`  tableb
ON tablea.product_id = tableb.product_id
AND tableb.category_id = {{OLDCATEGORY}}
WHERE tablea.`category_id` = {{NEWCATEGORY}} AND tableb.position is NULL

Just replace {{OLDCATEGORY}} with the ID of the category you are getting the products from and {{NEWCATEGORY}} is the ID of the category you are adding them to. This doesn’t delete the products out of the old category, as the client in question was planning on deleting this category anyway and that would cascade and remove the association.

about the author: "Blueclaw's Senior Technical SEO likes canonical tags, URL parameters and long walks on the beach (alright, site migrations). Can typically be found tinkering with the innards of the nearest eCommerce site."
filed under: Magento Tips | tags: