I have a child category and its parent category. How can I change parent category for this child category? Programatically, in a module helper.
1 Answer
Let's say $category
is your child category. And you want to make it a child of the category $parentCategory
.
You can call this:
$category->move($parentCategory->getId(), null);
-
? you've got to be kidding me! :) I will try, but I am open to other suggestions :) Apr 18, 2017 at 19:18
-
1why do you think I'm kidding? There is the
move
method: github.com/magento/magento2/blob/develop/app/code/Magento/…– Marius ♦Apr 18, 2017 at 19:20 -
will it properly move its products too, is my only concern... Apr 18, 2017 at 19:28
-
1the products associated to the child category remain associated to that category. But you might need to reindex though.– Marius ♦Apr 18, 2017 at 19:42
-
I am missing the second argument for the method ->move($parent, $afterChild). Do you know how can I get last child of the parent category? Apr 18, 2017 at 19:44