Antonino Bonumore
Antonino Bonumore

Reputation: 797

Copy Magento Categories to Parents

Scenario: A new blank Magento with a category product and customer import with some fixes to do.

Category structure:

Root
 L..Category_parent1 (0 products)
    L..Category_child1 (22)
    L..Category_child2 (34)
 L..Category_parent2 (0)
    L..Category_child1 (22)
    L..Category_child2 (34)
 L..Category_parent3 (0)
    L..Category_child1 (22)
    L..Category_child2 (0)
       L..Category_child2_child1 (22)
       L..Category_child2_child2 (34)
    L..Category_child3 (10)

I want to copy all products from a child category to its relative parent using a SQL query or a php script. (I don't know if it's possible to do this through the Magento admin).

Desired Result:

Root
 L..Category_parent1 (22 + 34 products)
    L..Category_child1 (22)
    L..Category_child2 (34)
 L..Category_parent2 (22 + 34)
    L..Category_child1 (22)
    L..Category_child2 (34)
 L..Category_parent3 (22 + 22 + 34 + 10)
    L..Category_child1 (22)
    L..Category_child2 (22 + 34)
       L..Category_child2_child1 (22)
       L..Category_child2_child2 (34)
    L..Category_child3 (10)

UPDATE! Is there a way to do this only on show products? See products in this way manteining relashionship with their own categories (simply do that in view layout in list...)?

Upvotes: 3

Views: 1030

Answers (2)

Antonino Bonumore
Antonino Bonumore

Reputation: 797

It seems that setting up is_anchor to all categories solve my problem, but the answer provided by joseph answer the question

Upvotes: 1

Joe Mastey
Joe Mastey

Reputation: 27119

As a partial solution:

insert into catalog_category_product_index (
    select cat.parent_id, prod.product_id, 1 
      from catalog_category_product_index prod, catalog_category_entity cat
      where prod.category_id = cat.entity_id and cat.level >= 1
);

This should select everything and bump it up a level (until we get to the root). A separate query would help the position column (currently hardcoded to 1). The big problem is, though, that your desired outcome actually bumps items more than one level, whereas this query only does one level. To really generalize properly, maybe drop this query into some code and repeat it starting at the lowest depth and move upwards.

Hope that helps!

Thanks, Joe

Upvotes: 2

Related Questions