Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No 301 redirects are generated for category imports #61

Open
koenner01 opened this issue Sep 25, 2017 · 1 comment
Open

No 301 redirects are generated for category imports #61

koenner01 opened this issue Sep 25, 2017 · 1 comment

Comments

@koenner01
Copy link
Contributor

When you rename a category during an import, no 301 redirects are generated for the old links during the import. (we are using M2.1.7)

I did some debugging and found that the function reindexUpdatedCategories() in \FireGento\FastSimpleImport\Model\Import\Category tries to do very similiar things as an observer \Magento\CatalogUrlRewrite\Observer\CategoryProcessUrlRewriteMovingObserver M2 is using.

It is my opinion that

protected function reindexUpdatedCategories($categoryId)
{
    /** @var $category \Magento\Catalog\Model\Category */
    $category = $this->defaultCategory->load($categoryId);
    //$categoryName = $category->getName();
    foreach ($category->getStoreIds() as $storeId) {
        if ($storeId == 0) {
            continue;
        }
        $category = $this->categoryRepository->get($categoryId, $storeId);
        $urlRewrites = $this->categoryUrlRewriteGenerator->generate($category, true);
        $this->urlPersist->replace($urlRewrites);
    }
    return $this;
}

should become

protected function reindexUpdatedCategories($categoryId)
{
    /** @var $category \Magento\Catalog\Model\Category */
    $category = $this->defaultCategory->load($categoryId);
    //$categoryName = $category->getName();
    foreach ($category->getStoreIds() as $storeId) {
        if ($storeId == 0) {
            continue;
        }
        $category = $this->categoryRepository->get($categoryId, $storeId);

        $saveRewritesHistory = $this->_scopeConfig->isSetFlag(
            \Magento\CatalogUrlRewrite\Block\UrlKeyRenderer::XML_PATH_SEO_SAVE_HISTORY,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $storeId
        );
        $category->setData('save_rewrites_history', $saveRewritesHistory);

        $urlRewrites = $this->categoryUrlRewriteGenerator->generate($category, true);
        $this->urlPersist->replace($urlRewrites);
    }
    return $this;
}

This way the functionality that M2 uses to generate 301 redirects between category renames is also applied for imports.

In the observer there is also a part that generates the category product urls but I fear this can create a significant increase in import time if that part would be added to the above code.

@koenner01
Copy link
Contributor Author

As I mentioned in the previous post, I did some testing with the extra part for the category product url 301 redirects and the code is in fact needed to generate correct 301 redirects after changing a category url.

The function looks like this now

protected function reindexUpdatedCategories($categoryId)
{
    /** @var $category \Magento\Catalog\Model\Category */
    $category = $this->defaultCategory->load($categoryId);
    //$categoryName = $category->getName();
    foreach ($category->getStoreIds() as $storeId) {
        if ($storeId == 0) {
            continue;
        }
        $category = $this->categoryRepository->get($categoryId, $storeId);
        $saveRewritesHistory = $this->_scopeConfig->isSetFlag(
            \Magento\CatalogUrlRewrite\Block\UrlKeyRenderer::XML_PATH_SEO_SAVE_HISTORY,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $storeId
        );
        $category->setData('save_rewrites_history', $saveRewritesHistory);
        $urlRewrites = array_merge(
            $this->categoryUrlRewriteGenerator->generate($category, true),
            $this->urlRewriteHandler->generateProductUrlRewrites($category)
        );
        $this->urlPersist->replace($urlRewrites);
    }
    return $this;
}

I had to add a new class to the constructor for the $urlRewriteHandler (\Magento\CatalogUrlRewrite\Observer\UrlRewriteHandler $urlRewriteHandler)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant