Skip to content

Commit

Permalink
Merge pull request #74 from MrGekko/category-urls-from-root
Browse files Browse the repository at this point in the history
Category urls from root
  • Loading branch information
peterjaap committed Nov 2, 2022
2 parents e24250e + 5028968 commit 9c6a82d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Or download and copy the `src` directory into `app/code/Elgentos/RegenerateCatal
```
Usage:
regenerate:product:url [-s|--store="..."] [pids1] ... [pidsN]
regenerate:category:url [-s]--store="..."] [cids1] ... [cidsN]
regenerate:category:path [-s]--store="..."] [cids1] ... [cidsN]
regenerate:category:url [-s]--store="..."] [-r]--root="..."] [cids1] ... [cidsN]
regenerate:category:path [-s]--store="..."] [-r]--root="..."] [cids1] ... [cidsN]
regenerate:cms-page:url [-s]--store="..."] [pids1] ... [pidsN]
Arguments:
Expand All @@ -32,6 +32,7 @@ Arguments:
Options:
--store (-s) Use a specific store (store Id, store code or 'all')
--root (-r) Regenerate for root category and its children, ignoring cids.
--help (-h) Display this help message
```

Expand All @@ -45,6 +46,9 @@ php bin/magento regenerate:product:url -s1 1 2 3 4

# Regenerate url for all CMS pages
php bin/magento regenerate:cms-page:url -s all

# Regenerate url for root category 4 and its children for store 1
php bin/magento regenerate:category:url -s1 -r4
```

## FAQ
Expand Down
19 changes: 16 additions & 3 deletions src/Console/Command/RegenerateCategoryPathCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\App\State;
Expand Down Expand Up @@ -53,6 +54,12 @@ protected function configure(): void
'cids',
InputArgument::IS_ARRAY,
'Categories to regenerate'
)->addOption(
'root',
'r',
InputOption::VALUE_OPTIONAL,
'Regenerate for root category and its children only',
false
);

parent::configure();
Expand Down Expand Up @@ -82,12 +89,18 @@ public function execute(InputInterface $input, OutputInterface $output)
foreach ($stores as $storeId) {
$categories = $this->categoryCollectionFactory->create()
->setStore($storeId)
->addAttributeToSelect(['name', 'url_path', 'url_key'])
->addAttributeToSelect(['name', 'url_path', 'url_key', 'path'])
->addAttributeToFilter('level', ['gt' => 1]);

$fromRootOnly = intval($input->getOption('root')) ?? 0;
$categoryIds = $input->getArgument('cids');

if (!empty($categoryIds)) {
if ($fromRootOnly) {
//path LIKE '1/rootcategory/%' OR path = '1/rootcategory'
$categories->addAttributeToFilter('path', [
'like' => '1/' . $fromRootOnly . '/%',
'=' => '1/' . $fromRootOnly
]);
} elseif (!empty($categoryIds)) {
$categories->addAttributeToFilter('entity_id', ['in' => $categoryIds]);
}

Expand Down
19 changes: 16 additions & 3 deletions src/Console/Command/RegenerateCategoryUrlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
Expand Down Expand Up @@ -59,6 +60,12 @@ protected function configure(): void
'cids',
InputArgument::IS_ARRAY,
'Categories to regenerate'
)->addOption(
'root',
'r',
InputOption::VALUE_OPTIONAL,
'Regenerate for root category and its children only',
false
);

parent::configure();
Expand Down Expand Up @@ -91,12 +98,18 @@ public function execute(InputInterface $input, OutputInterface $output): int

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

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

if (!empty($categoryIds)) {
if ($fromRootId) {
//path LIKE '1/rootcategory/%' OR path = '1/rootcategory'
$categories->addAttributeToFilter('path', [
'like' => '1/' . $fromRootId . '/%',
'=' => '1/' . $fromRootId
]);
} elseif (!empty($categoryIds)) {
$categories->addAttributeToFilter('entity_id', ['in' => $categoryIds]);
}

Expand Down

0 comments on commit 9c6a82d

Please sign in to comment.