Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BeforeAddressSaveObserver.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Customer\Helper\Address as HelperAddress;
14 
19 {
23  const VIV_CURRENTLY_SAVED_ADDRESS = 'currently_saved_address';
24 
28  protected $_customerAddress;
29 
33  protected $_coreRegistry;
34 
39  public function __construct(
40  HelperAddress $customerAddress,
41  Registry $coreRegistry
42  ) {
43  $this->_customerAddress = $customerAddress;
44  $this->_coreRegistry = $coreRegistry;
45  }
46 
53  public function execute(\Magento\Framework\Event\Observer $observer)
54  {
55  if ($this->_coreRegistry->registry(self::VIV_CURRENTLY_SAVED_ADDRESS)) {
56  $this->_coreRegistry->unregister(self::VIV_CURRENTLY_SAVED_ADDRESS);
57  }
58 
60  $customerAddress = $observer->getCustomerAddress();
61  if ($customerAddress->getId()) {
62  $this->_coreRegistry->register(self::VIV_CURRENTLY_SAVED_ADDRESS, $customerAddress->getId());
63  } else {
64  $configAddressType = $this->_customerAddress->getTaxCalculationAddressType();
65  $forceProcess = $configAddressType == AbstractAddress::TYPE_SHIPPING
66  ? $customerAddress->getIsDefaultShipping()
67  : $customerAddress->getIsDefaultBilling();
68  if ($forceProcess) {
69  $customerAddress->setForceProcess(true);
70  } else {
71  $this->_coreRegistry->register(self::VIV_CURRENTLY_SAVED_ADDRESS, 'new_address');
72  }
73  }
74  }
75 }
__construct(HelperAddress $customerAddress, Registry $coreRegistry)
$customerAddress