1

I'm new to Magento 2, no e-commerce experience before.

So I'm working on making this module that should 'add default shipping address to customer when they sign up'.

During some research and learning magento 2, I found this event and observer + dependency injection could help me able to get it happen.

BUT, my code is not working.. need some help!

here are my codes and structure.

my observer.

<?php
namespace Threeon\DefaultAddress\Observer;

use Magento\Framework\Event\ObserverInterface;

class AddressAdder implements ObserverInterface
{
protected $addressFactory;

public function __construct(Magento\Customer\Model\AddressFactory $addressFactory)
{
    $this->addressFactory = $addressFactory;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $customer = $observer->getEvent()->getData('customer');

    /*$addresses[] = $observer->getEvent()->getAddresses();*/
    $address = $this->addressFactory->create();

    $address->setCountryId('HR')
        //->setRegionId('1') //state/province, only needed if the country is USA
        ->setPostcode('31000')
        ->setCity('Osijek')
        ->setTelephone('0038511223344')
        ->setCompany('GMI')
        ->setStreet('NO:12 Lake View')
        ->setIsDefaultBilling('1')
        ->setIsDefaultShipping('1')
        ->setSaveInAddressBook('1');
    try{
        $address->save();
        $customer->setAddress($address);
    }
    catch (Exception $e) {
        Zend_Debug::dump($e->getMessage());
    }
    // TODO: Implement execute() method.
}

}

?>

event.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

1
  • I think it is better to insert code rather than images for search engines. Commented Aug 14, 2016 at 18:26

1 Answer 1

1

Find out that I was messing with wrong event :/

It works now with new event and modified code as this:

events.xml:

<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="AddressAdderObserver" instance="Threeon\DefaultAddress\Observer\AddressAdder"/>
  </event>
</config>

observer:

use Magento\Framework\Event\ObserverInterface;

class AddressAdder implements ObserverInterface
{
protected $manager;

public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)

{
    $this->manager = $objectManager;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $customer = $observer->getEvent()->getCustomer();

    $address = $this->manager->create('\Magento\Customer\Model\Address');
    $address = $address->setData('parent_id', $customer->getId())
        ->setData('is_active', '1')
        ->setData('city', 'ny')
        ->setData('company', 'ibuyr')
        ->setData('country_id', 'US')
        ->setData('firstname', $customer->getFirstName())
        ->setData('lastname', $customer->getLastName())
        ->setData('postcode', '07024')
        ->setData('region', 'New York')
        ->setData('region_id', '43')
        ->setData('street', '103 waterside')
        ->setData('telephone', '200-300-1111')
        ->setIsDefaultShipping('1');
    try{
        $address->save();
    }
    catch (Exception $e) {
        Zend_Debug::dump($e->getMessage());
    }
    return $this;
}
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.