After so much debugging, I've found an alternative solution for this issue by creating a custom module to generate the URL rewrite while creating/editing a product from Admin.
Create Vendor/UrlRewriteFix/etc/adminhtml/di.xml
and define the plugin class Magento\Catalog\Model\Product
in the file.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Product">
<plugin name="vendor_product_after_save" type="Vendor\UrlRewriteFix\Plugin\AfterProductSave" disabled="false" />
</type>
</config>
Now create the product's afterSave
plugin file in Vendor/UrlRewriteFix/Plugin/AfterProductSave.php
with the below content.
<?php
namespace Vendor\UrlRewriteFix\Plugin;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\UrlRewrite\Model\UrlFinderInterface;
class AfterProductSave
{
/**
* @var ProductUrlRewriteGenerator\Proxy
*/
private $urlRewriteGenerator;
/**
* @var UrlPersistInterface\Proxy
*/
private $urlPersist;
/**
* @var UrlRewrite\Proxy
*/
private $urlRewrite;
/**
* @var StoreManagerInterface\Proxy
*/
private $storeManager;
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var UrlFinderInterface
*/
protected $urlFinder;
/**
* AfterSave constructor.
* @param ProductUrlRewriteGenerator\Proxy $urlRewriteGenerator
* @param UrlPersistInterface\Proxy $urlPersist
* @param UrlRewrite\Proxy $urlRewrite
* @param StoreManagerInterface\Proxy $storeManager
* @param UrlFinderInterface $urlFinder
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ProductUrlRewriteGenerator\Proxy $urlRewriteGenerator,
UrlPersistInterface\Proxy $urlPersist,
UrlRewrite\Proxy $urlRewrite,
StoreManagerInterface\Proxy $storeManager,
UrlFinderInterface $urlFinder,
ScopeConfigInterface $scopeConfig
) {
$this->urlRewriteGenerator = $urlRewriteGenerator;
$this->urlPersist = $urlPersist;
$this->urlRewrite = $urlRewrite;
$this->storeManager = $storeManager;
$this->urlFinder = $urlFinder;
$this->scopeConfig = $scopeConfig;
}
/**
* @param \Magento\Catalog\Model\Product $_product
* @param $result
* @return mixed
*/
public function afterSave(\Magento\Catalog\Model\Product $_product, $result)
{
if ($this->isSingleStoreMode()) {
if (!$this->checkUrlRewrite($_product->getUrlKey())) {
$stores = $this->storeManager->getStores(false);
foreach ($stores as $store) {
$_product->setStoreId($store->getId());
$newUrls = $this->urlRewriteGenerator->generate($_product);
try {
$this->urlPersist->replace($newUrls);
} catch (\Exception $e) {
$this->log(sprintf('<error>Duplicated url for store ID %d, product %d (%s) - %s Generated URLs:' . PHP_EOL . '%s</error>' . PHP_EOL, $store->getId(), $_product->getId(), $_product->getSku(), $e->getMessage(), implode(PHP_EOL, array_keys($newUrls))));
}
}
}
}
return $result;
}
/**
* @return mixed
*/
protected function isSingleStoreMode()
{
return $this->scopeConfig->getValue('general/single_store_mode/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
}
/**
* @param $urlKey
* @return bool
*/
protected function checkUrlRewrite($urlKey)
{
$filterData = [
UrlRewrite::REQUEST_PATH => $urlKey.'.html'
];
$rewrite = $this->urlFinder->findOneByData($filterData);
if (!empty($rewrite)) {
$type = $rewrite->getEntityType();
$entityId = $rewrite->getEntityId();
if ($entityId && $type == 'product') {
return true;
}
return false;
}
return false;
}
}
Run the deployment commands after enabling the module.
I hope it'll help somebody facing the same issue.