I have 3000+ products in a brand category. How do I programmatically move brand to brand collection?

If I move in the backend it will take some time. How do I move?

  • 1
    is brand collection a category or else? – Amit Bera Sep 30 '15 at 7:00
  • Ya Both are category only – Magento 2 Sep 30 '15 at 7:03
  • have you tried with sql Query...? – Amit Bera Sep 30 '15 at 7:10
  • How to move using sql – Magento 2 Sep 30 '15 at 7:12
  • please see answer – Amit Bera Sep 30 '15 at 7:32

You can achieve this easily using a MySQL query.

As per as my suggestion this is the best way.

Magento saves product-category associations in the catalog_category_product table.


Step 1: Take a backup of the database. Mainly the catalog_category_product table.

Step 2: Run the below query for moving products from the old category to the new category.

SELECT {NEW_CAT_ID} cat, product_id, position FROM catalog_category_product WHERE category_id= {OLD_CAT_ID} AND product_id NOT IN ( SELECT product_id FROM catalog_category_product WHERE category_id = {NEW_CAT_ID}));

Suppose, your new category id is 7, and the old category id is 4.

INSERT INTO catalog_category_product(

    SELECT 7 cat, `product_id`, `position`
    FROM catalog_category_product
    WHERE `category_id` = 4
    AND product_id NOT IN
        SELECT  `product_id`
        FROM catalog_category_product
        WHERE category_id = 7

Step 3: Delete records from the old category using a MySQL query:

Delete from catalog_category_product where category_id={OLD_CAT_ID};


Delete from catalog_category_product where category_id=4;
  • ok i will try it . – Magento 2 Sep 30 '15 at 9:14
  • Hey it's moving one category to another category but it not display in front end i checked inventory, stock status everthing is ok but not display in front end\ – Magento 2 Oct 1 '15 at 5:39
  • after that old category product also not display frontend – Magento 2 Oct 1 '15 at 5:47
  • Please reindex from admin>system>configuration.... specifically category products – Amit Bera Oct 1 '15 at 5:53
  • step are removing the product from old category – Amit Bera Oct 1 '15 at 5:54

Let's say you have the category you want to move called $category and the category you want to be the parent of $category...Let's call that $parentCategory.
you can do this from code:

Mage::getResourceSingleton('catalog/category')->changeParent($category, $parentCategory);

This will make $category a child of $parentCategory but it will add it as the first child of $parentCategory.
If you want to move $category after a certain chilc of $parentCategory you need to do this:

Mage::getResourceSingleton('catalog/category')->changeParent($category, $parentCategory, $afterChildId);

where $afterChildId is the id of a child category of $parentCategory.

Here is an example.
You have this tree

 - A
 - B
   - C
   - D

And you want to make A a child of B.

Mage::getResourceSingleton('catalog/category')->changeParent($instanceOfCategoryA, $instanceOfCategoryB);

will result in

 - B
   - A
   - C
   - D


Mage::getResourceSingleton('catalog/category')->changeParent($instanceOfCategoryA, $instanceOfCategoryB, $idOfCategoryC);

will result in

 - B
   - C
   - A
   - D
  • can you please give the full code and tell me where i have to use this code – Magento 2 Sep 30 '15 at 7:14
  • you can create a module and do this in the install script of the module. Here is a tutorial on how to do that: inchoo.net/magento/… – Marius Sep 30 '15 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.