3

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?

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

3 Answers 3

4

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.

Steps

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};

Example:

Delete from catalog_category_product where category_id=4;
8
  • ok i will try it .
    – Magento 2
    Sep 30, 2015 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, 2015 at 5:39
  • after that old category product also not display frontend
    – Magento 2
    Oct 1, 2015 at 5:47
  • Please reindex from admin>system>configuration.... specifically category products
    – Amit Bera
    Oct 1, 2015 at 5:53
  • step are removing the product from old category
    – Amit Bera
    Oct 1, 2015 at 5:54
3

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

and

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

will result in

 - B
   - C
   - A
   - D
2
  • can you please give the full code and tell me where i have to use this code
    – Magento 2
    Sep 30, 2015 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, 2015 at 8:16
0

Here is the query of Amit Bera for Magento 2

 INSERT INTO catalog_category_product (category_id, product_id, position)
    SELECT 100, `product_id`, `position`
    FROM catalog_category_product
    WHERE `category_id` = 726
    AND product_id NOT IN
    (
        SELECT  `product_id`
        FROM catalog_category_product
        WHERE category_id = 100
    )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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