diff --git a/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php b/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php new file mode 100644 index 0000000..4e57645 --- /dev/null +++ b/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php @@ -0,0 +1,168 @@ +urlRewriteFactory = $urlRewriteFactory; + $this->storeManager = $storeManager; + $this->_categoryHelper = $categoryHelper; + $this->_categoryFactory = $categoryFactory; + $this->scopeConfig = $scopeConfig; + $this->categoryCollectionFactory = $categoryCollectionFactory; + parent::__construct($name); + } + + /** + * Configure the command + */ + protected function configure() + { + $this->setName('iazel:regenerate_urls'); + $this->setDescription('Regenerate Url\'s for categories'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + foreach ($this->storeManager->getStores() as $store) { + echo $store->getCode() . ':'; + $this->_category_suffixes[$store->getId()] = $this->scopeConfig->getValue( + 'catalog/seo/category_url_suffix', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $store->getId() + ); + $this->getChildrenCategories($store); + echo "\n"; + } + } + + protected function insertIntoRewrites($id, $url_key, $store_id) + { + $urlRewrite = $this->urlRewriteFactory->create(); + $urlRewrite->addData([ + 'entity_type' => \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite::ENTITY_TYPE_CATEGORY, + 'entity_id' => $id, + 'request_path' => $url_key, + 'target_path' => 'catalog/category/view/id/' . $id, + 'redirect_type' => 0, + 'store_id' => $store_id, + 'description' => null, + 'is_autogenerated' => 1, + 'metadata' => null + ] + ); + try { + $urlRewrite->getResource()->save($urlRewrite); + echo '.'; + } catch (AlreadyExistsException $alreadyExistsException) { + echo '-'; + } + return $this; + } + + public function getChildrenCategories($store, $current_category = null) + { + if (is_null($current_category)) { + $parent_id = $store->getRootCategoryId(); + } else { + $parent_id = $current_category->getId(); + $this->_categoryUrlKeys[$current_category->getId()] = $current_category->getUrlKey(); + $path_exploded = explode('/', $current_category->getPath()); + $complete_path = []; + foreach ($path_exploded as $path) { + if ($path == 1 || $path == $store->getRootCategoryId()) { + continue; + } + $complete_path[] = $this->_categoryUrlKeys[$path]; + } + $complete_path = implode('/', $complete_path) . $this->_category_suffixes[$store->getId()]; + $this->insertIntoRewrites($current_category->getId(), $complete_path, $store->getId()); + } + $category = $this->_categoryFactory->create(); + $categoryTmp = $this->_categoryFactory->create()->load($parent_id); + $recursionLevel = 0; + + $collection = $this->categoryCollectionFactory->create() + ->addPathsFilter($categoryTmp->getPath().'/') + ->addLevelFilter($categoryTmp->getLevel() + $recursionLevel + 1) + ->addAttributeToSelect('url_key'); + + if (count($collection)) { + foreach ($collection as $child) { + $this->getChildrenCategories($store, $child); + } + } + return $this; + } + +} \ No newline at end of file diff --git a/Iazel/RegenProductUrl/etc/di.xml b/Iazel/RegenProductUrl/etc/di.xml index 08d0b29..8cf4e91 100644 --- a/Iazel/RegenProductUrl/etc/di.xml +++ b/Iazel/RegenProductUrl/etc/di.xml @@ -10,7 +10,15 @@ Iazel\RegenProductUrl\Console\Command\RegenerateProductUrlCommand + Iazel\RegenProductUrl\Console\Command\RegenerateCategoryUrlCommand + + + Magento\Framework\App\State\Proxy + Magento\Catalog\Model\ResourceModel\Product\Collection\Proxy + Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator\Proxy + +