diff --git a/README.md b/README.md index ae35110..3480aeb 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 ``` @@ -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 diff --git a/src/Console/Command/RegenerateCategoryPathCommand.php b/src/Console/Command/RegenerateCategoryPathCommand.php index d050bfe..cc18f8d 100644 --- a/src/Console/Command/RegenerateCategoryPathCommand.php +++ b/src/Console/Command/RegenerateCategoryPathCommand.php @@ -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; @@ -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(); @@ -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]); } diff --git a/src/Console/Command/RegenerateCategoryUrlCommand.php b/src/Console/Command/RegenerateCategoryUrlCommand.php index d577b8f..540598b 100644 --- a/src/Console/Command/RegenerateCategoryUrlCommand.php +++ b/src/Console/Command/RegenerateCategoryUrlCommand.php @@ -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; @@ -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(); @@ -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]); }