Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Allow to generate categories #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions Iazel/RegenProductUrl/Console/Command/RegenerateCategoryUrlCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
namespace Iazel\RegenProductUrl\Console\Command;

use Magento\Framework\Exception\AlreadyExistsException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class RegenerateUrls.php
*/
class RegenerateCategoryUrlCommand extends Command
{

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/**
* @var \Magento\UrlRewrite\Model\UrlRewriteFactory
*/
protected $urlRewriteFactory;

/**
* @var \Magento\Catalog\Helper\Category
*/
protected $_categoryHelper;

/**
* @var \Magento\Catalog\Model\CategoryFactory
*/
protected $_categoryFactory;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;

/**
* [category_id] = url_key
*
* @var array
*/
protected $_categoryUrlKeys = [];

/**
* [store_id] = category_suffix
*
* @var array
*/
protected $_category_suffixes = [];

protected $categoryCollectionFactory;

/**
* RegenerateUrls constructor.
*
* @param \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
* @param string $name
*/
public function __construct(
\Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\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;
$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;
}

}
8 changes: 8 additions & 0 deletions Iazel/RegenProductUrl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@
<arguments>
<argument name="commands" xsi:type="array">
<item name="product_url_regeneration_command" xsi:type="object">Iazel\RegenProductUrl\Console\Command\RegenerateProductUrlCommand</item>
<item name="category_url_regeneration_command" xsi:type="object">Iazel\RegenProductUrl\Console\Command\RegenerateCategoryUrlCommand</item>
</argument>
</arguments>
</type>
<type name="Iazel\RegenProductUrl\Console\Command\RegenerateProductUrlCommand">
<arguments>
<argument name="state" xsi:type="object">Magento\Framework\App\State\Proxy</argument>
<argument name="collection" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Collection\Proxy</argument>
<argument name="productUrlRewriteGenerator" xsi:type="object">Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator\Proxy</argument>
</arguments>
</type>
</config>