1

I installed Magento 2.1.3 enterprise edition. I created new category and a new product. It shows frontend correctly with given url_key under SEO tab. Now I need to change the url_key. When I change, It doesn't reflect in frontend, old url_key is used to display the product. But it displays updated value in back-end.

NOTE

I tried to update attributes in catalog grid actions option. But nothing happen.

UPDATE

If I change url_key and save manually, url_rewrite table creates the new url with 301 permanent redirection. But when I update url_key via script it's not working.

So please help me to solve this issue.

Thanks in advance.

0

This solve for CE, it can doesn't approach for EE.

If you modify url_key attribute in your own script directly (without saving product), you need to add code for the rewrite creating manually.

Magento add this functionality after saving product in observer:

Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver

public function execute(\Magento\Framework\Event\Observer $observer){        
    $product = $observer->getEvent()->getProduct();

    if ($product->dataHasChangedFor('url_key')
        || $product->getIsChangedCategories()
        || $product->getIsChangedWebsites()
        || $product->dataHasChangedFor('visibility')
    ) {
        $this->urlPersist->deleteByData([
            UrlRewrite::ENTITY_ID => $product->getId(),
            UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
            UrlRewrite::REDIRECT_TYPE => 0,
            UrlRewrite::STORE_ID => $product->getStoreId()
        ]);

        if ($product->isVisibleInSiteVisibility()) {
            $this->urlPersist->replace($this->productUrlRewriteGenerator->generate($product));
        }
    }
}

You should add this code to your own script with some changes, of course.

  • I saved product manually with different url_key, but this also not updated in url_rewrite table. – Vignesh Bala Mar 16 '17 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.