I have a child category and its parent category. How can I change parent category for this child category? Programatically, in a module helper.


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);
| improve this answer | |
  • ? you've got to be kidding me! :) I will try, but I am open to other suggestions :) – Vladimir Despotovic Apr 18 '17 at 19:18
  • 1
    why do you think I'm kidding? There is the move method: github.com/magento/magento2/blob/develop/app/code/Magento/… – Marius Apr 18 '17 at 19:20
  • will it properly move its products too, is my only concern... – Vladimir Despotovic Apr 18 '17 at 19:28
  • 1
    the products associated to the child category remain associated to that category. But you might need to reindex though. – Marius Apr 18 '17 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? – Vladimir Despotovic Apr 18 '17 at 19:44

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.