1

I have the following scenario:

  1. I have created a new product.
  2. I have left the url-key field blank.
  3. If url-key field kept blank, then on product saving, it will set url key value automatically based on the product title.
  4. The url-key value is generated for the new product as expected.
  5. Now I have edited the same product and changed the title. Cleared the url-key field value.
  6. This time on saving it won't set the new url key value automatically based on the new product title.

Steps to replicate:

  1. Create a new product
  2. Enter title, and other details except for url key. Keep url key blank.
  3. Save the product.
  4. Now edit the same product.
  5. Change the title of the product which will be different than previous
  6. Clear the url key field value.
  7. save the product

Actual result: Url key value is blank

Expected result: url key value should be updated as per new product title

Is this a Magento's default behavior or its a core bug on the product edit?

  • Is this a Magento's default behaviour or its a core bug on the product edit? – 1990rk4 May 7 at 6:38
0

this is a native magento 2 default, url will be changed , if you duplicate product ( it will add to url index of copy, 1,2,3...etc.) If you want to create new product url on save , you need to create a simple module for this reason. You can create Observer

catalog_product_save_after

And then :

      <?php

        namespace [VendorName]\[ModuleName]\Observer\Backend;


        class AfterProductSaveObserver implements \Magento\Framework\Event\ObserverInterface{


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


            public function __construct(
                \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
            )
            {

                $this->_storeManager = $storeManagerInterface;
            }

            /**
             * Product after save event handler
             *
             * @param \Magento\Framework\Event\Observer $observer
             * @return void
             * @SuppressWarnings(PHPMD.CyclomaticComplexity)
             */
            public function execute(\Magento\Framework\Event\Observer $observer)
            {
        if ($actionInstance = Mage::app()->getFrontController()->getAction()) {
                $action = $actionInstance->getFullActionName();
                if ($action == 'adminhtml_catalog_product_save')  //check if this admin event
{
     /** @var $product \Magento\Catalog\Model\Product */
                $product = $observer->getEvent()->getProduct(); // get current product
                $urlKey = $this->_storeManager->getStore(1)->getBaseUrl() . $product->getUrlKey();// it will return the product url

// Put here your`s save code for new URL

            }
                }
            }

        }

If it help you , please vote up .

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.