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">