1

I am trying to add a new value "merchant_id" in

\module-reports\Observer\CatalogProductViewObserver.php

How i can override save by add new data to save to it

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

        $viewData['product_id'] = $productId;
        $viewData['store_id']   = $this->_storeManager->getStore()->getId();
        if ($this->_customerSession->isLoggedIn()) {
            $viewData['customer_id'] = $this->_customerSession->getCustomerId();
        } else {
            $viewData['visitor_id'] = $this->_customerVisitor->getId();
        }

        $this->_productIndxFactory->create()->setData($viewData)->save()->calculate();

        $this->eventSaver->save(\Magento\Reports\Model\Event::EVENT_PRODUCT_VIEW, $productId);
    } 

I Need to add $viewData['merchant_id'] and assign value to it .

2

There are two options:

1) Disable the default observer and add our custom observer - events.xml:

 <?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_controller_product_view">
            <observer name="reports" disabled="true"/> <!--Disable default observer-->
            <!--Add our custom observer-->
            <observer name="reports_custom" 
              instance="Vendor\Module\Observer\CatalogProductViewObserver" />
        </event>
    </config>

2) Can override - preference - di.xml

<preference for="Magento\Reports\Observer\CatalogProductViewObserver" type="Vendor\Module\Observer\CatalogProductViewObserver" />

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.