Skip to content

Commit

Permalink
Merge pull request #81 from marvinhinz/patch-1
Browse files Browse the repository at this point in the history
improve RegenerateCategoryUrlCommand to only generate rewrites for categories of the correct root category
  • Loading branch information
peterjaap authored Mar 18, 2024
2 parents 9abb064 + 597f4fa commit b570189
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Console/Command/RegenerateCategoryUrlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,38 @@ public function execute(InputInterface $input, OutputInterface $output): int

$stores = $this->getChosenStores();

$rootIdOption = intval($input->getOption('root')) ?: false;

foreach ($stores as $storeId) {
$currentRootId = intval($this->storeManager->getGroup($this->storeManager->getStore($storeId)->getStoreGroupId())->getRootCategoryId());
if($rootIdOption !== false) {
$fromRootId = $rootIdOption;
if($rootIdOption !== $currentRootId) {
$output->writeln(
sprintf('Skipping store %s because its root category id %s, differs from the passed root category %s', $storeId, $currentRootId, $fromRootId)
);
continue;
}
} else {
$fromRootId = $currentRootId;
}

$output->writeln(
sprintf('Processing store %s...', $storeId)
);

$rootCategory = $this->categoryCollectionFactory->create()->addAttributeToFilter('entity_id', $fromRootId)->addAttributeToSelect("name")->getFirstItem();
if(!$rootCategory->getId()) {
throw new \Exception(sprintf("Root category with ID %d, was not found.", $fromRootId));
}
$this->emulation->startEnvironmentEmulation($storeId, Area::AREA_FRONTEND, true);

$categories = $this->categoryCollectionFactory->create()
->setStore($storeId)
->addAttributeToSelect(['name', 'url_path', 'url_key', 'path'])
->addAttributeToFilter('level', ['gt' => 1]);

$fromRootId = intval($input->getOption('root')) ?? 0;

$categoryIds = $input->getArgument('cids');
if ($fromRootId) {
//path LIKE '1/rootcategory/%' OR path = '1/rootcategory'
Expand All @@ -115,7 +138,12 @@ public function execute(InputInterface $input, OutputInterface $output): int

foreach ($categories as $category) {
$output->writeln(
sprintf('Regenerating urls for %s (%s)', $category->getName(), $category->getId())
sprintf('Regenerating urls for %s (%s)',
implode('/', [
$rootCategory->getName(),
...array_map(fn($category) => $category->getName(), $category->getParentCategories())
]),
$category->getId())
);

$this->urlPersist->deleteByData(
Expand Down

0 comments on commit b570189

Please sign in to comment.