How to override observers in Magento 2?
I got it working with the help of preference itself. Here is the code which might be helpful to someone
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver" type="Aceturtle\ExcludeSimpleProductUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver" />
</config>
ProductProcessUrlRewriteSavingObserver.php
<?php
namespace Aceturtle\ExcludeSimpleProductUrlRewrite\Observer;
use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\Framework\Event\ObserverInterface;
class ProductProcessUrlRewriteSavingObserver extends \Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver
{
/**
* @var ProductUrlRewriteGenerator
*/
protected $productUrlRewriteGenerator;
/**
* @var UrlPersistInterface
*/
protected $urlPersist;
/**
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param UrlPersistInterface $urlPersist
*/
public function __construct(
ProductUrlRewriteGenerator $productUrlRewriteGenerator,
UrlPersistInterface $urlPersist
) {
$this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
$this->urlPersist = $urlPersist;
}
/**
* Generate urls for UrlRewrite and save it in storage
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var Product $product */
$product = $observer->getEvent()->getProduct();
if ($product->dataHasChangedFor('url_key')
|| $product->getIsChangedCategories()
|| $product->getIsChangedWebsites()
|| $product->dataHasChangedFor('visibility')
) {
if($product->getTypeId() == 'simple')
{
return;
}
$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 are trying to set preference on observer, which is not the right way. You need to create an observer on event "catalog_product_save_after". It would be working fine. Remove your existing preference from di.xml and create event.xml in your module /etc folder with below code
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_save_after">
<observer name="your_process_url_rewrite_saving" instance="Aceturtle\ExcludeSimpleProductUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver"/>
</event>
Your observer file will remain same as you are using in above post:
<?php
namespace Aceturtle\ExcludeSimpleProductUrlRewrite\Observer;
.....
class ProductProcessUrlRewriteSavingObserver extends \Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver
{
....
public function execute(\Magento\Framework\Event\Observer $observer){
if($product->getTypeId() == 'simple')
{
return;
}
.....
} }