I need to send data to an external API when a customer (I mean a logged in customer) add a product to wishlist.
I found the event: wishlist_add_product.
$this->_eventManager->dispatch(
'wishlist_add_product',
['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
);
This event is not related to a customer, this is also triggered when a guest user add a product to wishlist.
In my Observer I have to check if the user is logged in, then I have to load some customer details and I can call the API.
The question is:
How do I get the customer ID if it is authenticated, when the customer object is not present in the dispatch?
app/code/Vendor/Module/etc/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="wishlist_add_product">
<observer name="my_wishlist_add_product" instance="Vendor\Module\Observer\MyObserver" />
</event>
</config>
app/code/Vendor/Module/Observer/MyObserver.php
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class MyObserver implements ObserverInterface{
public function execute(Observer $observer){
// here I need something for load the logged in customer (if it is logged in)
}
}