Skip to main content
Update answer
Source Link
Msquare
  • 9.3k
  • 7
  • 27
  • 68
<?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="controller_action_predispatch_wishlist_index_index">name="wishlist_add_item">
       <observer name="MyObserverName" instance="Vendor\Module\Observer\MyObserver" />
   </event>
</config>
<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class MyObserver implements ObserverInterface
{
    protected $customerSession;
    
    public function __construct(
        .................................................
        .................................................
        \Magento\Customer\Model\SessionFactory $customerSession,
        .................................................
        .................................................
    ) {
        .................................................
        .................................................
        $this->_customerSession = $customerSession->create();
        .................................................
        .................................................
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($this->_customerSession->isLoggedIn()) {
            $cusomerid =  $this->_customerSession->getId();
            print_r($cusomerid);
            exit();
        }
    }
}
<?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="controller_action_predispatch_wishlist_index_index">
       <observer name="MyObserverName" instance="Vendor\Module\Observer\MyObserver" />
   </event>
</config>
<?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="wishlist_add_item">
       <observer name="MyObserverName" instance="Vendor\Module\Observer\MyObserver" />
   </event>
</config>
<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class MyObserver implements ObserverInterface
{
    protected $customerSession;
    
    public function __construct(
        .................................................
        .................................................
        \Magento\Customer\Model\SessionFactory $customerSession,
        .................................................
        .................................................
    ) {
        .................................................
        .................................................
        $this->_customerSession = $customerSession->create();
        .................................................
        .................................................
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($this->_customerSession->isLoggedIn()) {
            $cusomerid =  $this->_customerSession->getId();
            print_r($cusomerid);
            exit();
        }
    }
}
Source Link
Msquare
  • 9.3k
  • 7
  • 27
  • 68

Add This line in 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="controller_action_predispatch_wishlist_index_index">
       <observer name="MyObserverName" instance="Vendor\Module\Observer\MyObserver" />
   </event>
</config>

this observer file run before this wishlist redirect to wishlist page.

In your MyObserver.php first check if customer is login or not and then your logic.

I Hope This Helps You.