Created
November 24, 2016 15:47
-
-
Save Tjitse-E/4a7eca4aaa79c905be97cc9086793a4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Mynamespace\RegenerateUrls\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 RegenerateUrls 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=[]; | |
/** | |
* 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, | |
$name = 'regenerate_category_urls' | |
) | |
{ | |
$this->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('gb: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(); | |
$this->_categoryUrlKeys[$store->getId().'_'.$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[] = $this->_categoryUrlKeys[$store->getId()."_".$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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment