tags:

views:

54

answers:

2

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...)?

+1  A: 

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

Joseph Mastey
that works, but I think that it's better to do that on view mode (for some practical reason: move all products(dropping category) in a category without care about where this product it's alredy putted in)
Emergento
A: 

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

Emergento