10 use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
27 private $scopeResolver;
32 private $categoriesFactory;
46 $this->categoriesFactory = $categoriesFactory;
58 }
elseif ($this->isAdminStore()) {
61 $result = $this->categoryTree->getTree($this->categoryTree->getRootNode(
$category), $depth);
70 private function isAdminStore()
80 private function getScopeResolver()
82 if ($this->scopeResolver ==
null) {
84 ->get(\
Magento\Framework\
App\ScopeResolverInterface::class);
87 return $this->scopeResolver;
95 private function getTopLevelCategory()
97 $categoriesCollection = $this->categoriesFactory->create();
98 return $categoriesCollection->addFilter(
'level', [
'eq' => 0])->getFirstItem();
104 public function move($categoryId, $parentId, $afterId =
null)
106 $model = $this->categoryRepository->get($categoryId);
107 $parentCategory = $this->categoryRepository->get($parentId);
109 if ($parentCategory->hasChildren()) {
110 $parentChildren = $parentCategory->getChildren();
111 $categoryIds = explode(
',', $parentChildren);
112 $lastId = array_pop($categoryIds);
113 $afterId = ($afterId ===
null || $afterId > $lastId) ? $lastId : $afterId;
116 if (strpos($parentCategory->getPath(),
$model->getPath()) === 0) {
117 throw new \Magento\Framework\Exception\LocalizedException(
118 __(
'Operation do not allow to move a parent category to any of children category')
122 $model->move($parentId, $afterId);
123 }
catch (\Exception $e) {
124 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Could not move category'));
136 $categories->addAttributeToFilter(
'parent_id', [
'gt' => 0]);
elseif(isset( $params[ 'redirect_parent']))
__construct(\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository, \Magento\Catalog\Model\Category\Tree $categoryTree, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoriesFactory)
getTree($rootCategoryId=null, $depth=null)
move($categoryId, $parentId, $afterId=null)