0

I would like to customize the add to wish list functionality from my custom module In magento 2.

Actually i need custom options chosen and product id when i wishlist a product. i have found a link for list of magento 2 events below https://cyrillschumacher.com/magento2-list-of-all-dispatched-events/ please suggest me how to achieve this?

I have written code as shown below the event has been calling perfect but i unable to get the product id and custom options how to get in the observer?

app/code/Demo/Pricecalculations/etc/frontend/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="Demo_Pricecalculations_Wishlistproduct" instance="Demo\Pricecalculations\Observer\Wishlistproduct" />
   </event>
</config>

app/code/Demo/Pricecalculations/Observer/

<?php
namespace Demo\Pricecalculations\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
class Wishlistproduct implements \Magento\Framework\Event\ObserverInterface
{

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
        $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/Wishlist.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info("custom Wishlist function has been Calling");

    }

}
2

wishlist_add_product give us 3 parameters

    $this->_eventManager->dispatch(
            'wishlist_add_product',
            ['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
        );
  • wishlist : it give customer wishlist object.. At observer you can get wish object by $observer->getEvent()->getWishlist();
  • product: it give customer just added product object. At observer you can get product object by $observer->getEvent()->getProduct();
  • item: current add wishtlist item . At observer you can get product object by $observer->getEvent()->getItem();

So, at observe you can product id from $observer->getEvent()->getProduct()->getId()

  • Wonderful, you saved my day! – Mike Nguyen Apr 13 '18 at 8:11

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.