0

I've read through countless posts with similar issues and have made sure I didn't have the same errors.

I'm trying to use the customer_register_success event to trigger an observer that will fetch an image from Gravatar and save it on my server. The code for saving it to my server works fine, as I've tested it. The issue is the fact that the observer isn't being called when a customer registers.

config.xml

<config>
<modules>
    <Namespace_Getavatar>
        <version>1.0.0</version>
    </Namespace_Getavatar>
</modules>

<global>
  <models>
        <getavatar>
            <class>Namespace_Getavatar_Model</class>
        </getavatar>
    </models>
    <events>
        <customer_register_success>
            <observers>
                <Namespace_Getavatar_Model_Observer>
                    <type>singleton</type>
                    <class>Namespace_Getavatar_Model_Observer</class>
                    <method>setAvatar</method>
                </Namespace_Getavatar_Model_Observer>
            </observers>
        </customer_register_success>
    </events>
</global>

Observer.php

class Namespace_Getavatar_Model_Observer {

public function setAvatar(Varien_Event_Observer $observer)
{
    //fetching and saving avatar image

}

}

Namespace_Getavatar.xml

<config>
  <modules>
    <Namespace_Getavatar>
        <active>true</active>
        <codePool>local</codePool>
    </Namespace_Getavatar>
  </modules>
</config>

I'm using a username plugin, but I don't think that could interfere with the event itself?

0

Step 1: Dispatch event

In Magento 2, you can dispatch the event from any place: controller, model, block, data… We will create an event from controller file.

File directory: app/code/vendor/module_name/Controller/Index/Index.php

<?php
 namespace Vendor\Module_name\Controller\Index;

 class Index extends \Magento\Framework\App\Action\Action
 {
   public function execute()
   {
      $hello = new \Magento\Framework\DataObject(array('label' => Hello Magento 2));
      $this->_eventManager->dispatch('hello_magento_display', ['display' => $hello]);
      echo $hello->getDisplay();
      exit;
   }
 }

‘hello_magento_display’: event name

[‘display’ => $hello]: event argument

Open your browser and use link: http://yoursite.com/hellomagento

Result: Hello Magento 2

Step 2: Create events.xml file to catch event

File directory: app/code/vendor/module_name/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="customer_register_success">
        <observer name="change_hello_magento_display" instance="vednor\module_name\Observer\Change" />
    </event>
 </config>

Step 3: Create Observer file to catch event

File: app/code/vendor/module_name/Observer/Change.php

<?php
namespace vendor\module_name\Observer;

class Change implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $displayText = $observer->getData('display');
       $displayText->setDisplay('Catch magento 2 event successfully!!!');
       return $this;
    }
}

Use the link again on your browser: http://yoursite.com/hellomagento Result was changed: Catch magento 2 event successfully!!!

  • Questions is about M1. – sv3n Sep 12 at 13:20

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.