0

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 1

3

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);
7
  • ? you've got to be kidding me! :) I will try, but I am open to other suggestions :) Apr 18, 2017 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, 2017 at 19:20
  • will it properly move its products too, is my only concern... Apr 18, 2017 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, 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

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.