From 9f0371564b03dd564232881411cf50e0689ed752 Mon Sep 17 00:00:00 2001 From: Pascal Brouwers Date: Fri, 31 Mar 2017 14:28:17 +0200 Subject: [PATCH 1/3] Allow to generate categories --- .../Command/RegenerateCategoryUrlCommand.php | 158 ++++++++++++++++++ Iazel/RegenProductUrl/etc/di.xml | 1 + 2 files changed, 159 insertions(+) create mode 100644 Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php diff --git a/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php b/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php new file mode 100644 index 0000000..7c254ae --- /dev/null +++ b/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php @@ -0,0 +1,158 @@ +urlRewriteFactory = $urlRewriteFactory; + $this->storeManager = $storeManager; + $this->_categoryHelper = $categoryHelper; + $this->_categoryFactory = $categoryFactory; + $this->scopeConfig = $scopeConfig; + 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->getPathId()); + $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(); + $recursionLevel = 0; + $childrenCategories = $category->getCategories($parent_id, $recursionLevel, false, false, true); + if (count($childrenCategories)) { + foreach ($childrenCategories 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..aae7736 100644 --- a/Iazel/RegenProductUrl/etc/di.xml +++ b/Iazel/RegenProductUrl/etc/di.xml @@ -10,6 +10,7 @@ Iazel\RegenProductUrl\Console\Command\RegenerateProductUrlCommand + Iazel\RegenProductUrl\Console\Command\RegenerateCategoryUrlCommand From 1c6bce4fe9b1ae5f8b72e6d933c3a43839aba3b9 Mon Sep 17 00:00:00 2001 From: Andrew Howden Date: Thu, 5 Oct 2017 15:29:24 +0200 Subject: [PATCH 2/3] Proxy the dependencies in the URL generation command With Magento 2.1.8+ CLI commands are constructed when the CLI client is invoked. Additionally, DI will construct any objects that those commands type in their constructors, so on and so fourth down the stack. The problem is, this extension may exist in an environment in which the store is not yet installed, and the database is thus unavailable. A common place this is used is in 2.1.x in CI, as the database is required in order to generate static content. This commit proxies the dependencies of this constructor, such that the constructor can be used (and thus appear in the CLI menu) but not invoked while the store is not installed. --- Iazel/RegenProductUrl/etc/di.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Iazel/RegenProductUrl/etc/di.xml b/Iazel/RegenProductUrl/etc/di.xml index aae7736..8cf4e91 100644 --- a/Iazel/RegenProductUrl/etc/di.xml +++ b/Iazel/RegenProductUrl/etc/di.xml @@ -14,4 +14,11 @@ + + + Magento\Framework\App\State\Proxy + Magento\Catalog\Model\ResourceModel\Product\Collection\Proxy + Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator\Proxy + + From 1e4863add36f26f567fd632c09524952fae9540e Mon Sep 17 00:00:00 2001 From: Pascal Brouwers Date: Thu, 14 Jun 2018 11:29:36 +0200 Subject: [PATCH 3/3] Fix regenerating categories when category has no childs --- .../Command/RegenerateCategoryUrlCommand.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php b/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php index 7c254ae..4e57645 100644 --- a/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php +++ b/Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php @@ -51,6 +51,8 @@ class RegenerateCategoryUrlCommand extends Command */ protected $_category_suffixes = []; + protected $categoryCollectionFactory; + /** * RegenerateUrls constructor. * @@ -65,6 +67,7 @@ public function __construct( \Magento\Catalog\Helper\Category $categoryHelper, \Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, + \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, $name = 'regenerate_category_urls' ) { $this->urlRewriteFactory = $urlRewriteFactory; @@ -72,6 +75,7 @@ public function __construct( $this->_categoryHelper = $categoryHelper; $this->_categoryFactory = $categoryFactory; $this->scopeConfig = $scopeConfig; + $this->categoryCollectionFactory = $categoryCollectionFactory; parent::__construct($name); } @@ -133,7 +137,7 @@ public function getChildrenCategories($store, $current_category = null) } else { $parent_id = $current_category->getId(); $this->_categoryUrlKeys[$current_category->getId()] = $current_category->getUrlKey(); - $path_exploded = explode('/', $current_category->getPathId()); + $path_exploded = explode('/', $current_category->getPath()); $complete_path = []; foreach ($path_exploded as $path) { if ($path == 1 || $path == $store->getRootCategoryId()) { @@ -145,10 +149,16 @@ public function getChildrenCategories($store, $current_category = null) $this->insertIntoRewrites($current_category->getId(), $complete_path, $store->getId()); } $category = $this->_categoryFactory->create(); + $categoryTmp = $this->_categoryFactory->create()->load($parent_id); $recursionLevel = 0; - $childrenCategories = $category->getCategories($parent_id, $recursionLevel, false, false, true); - if (count($childrenCategories)) { - foreach ($childrenCategories as $child) { + + $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); } }