6

I was trying to override execute function of Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver using preference in di.xml

<preference for="Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver" 
type="Aceturtle\ExcludeSimpleProductUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver" />

But it doesn't seem to be firing the execute method mentioned in e thoverridden class. I want to exclude/skip simple product urls to be saved in url_rewrite table which is done in above observer. What is the process to override an observer file? Is there a different process I can follow to skip records for simple products to be saved in url_rewrite table to have less table size(Simple products are not visible anywhere on the site).

My observer program looks like

 <?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;
        }
    .....
    } }
  • Does below solution solve your problem? – Himmat Paliwal Apr 7 '17 at 4:45
9

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));
            }
        }
    }
}
1

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;
    }
.....
} }
  • It is calling main execute function of Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver too. I want to completely override this file. – amitshree Apr 7 '17 at 6:05
  • Yes, it will call that too. Why do you want to disable that function? That is magento default functionality, if you want moderate any data than you can do that in your observer file. – Himmat Paliwal Apr 7 '17 at 7:20
  • I do not want simple product urls to be saved in url_rewrite table which is done in main execute function. – amitshree Apr 7 '17 at 7:43

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.