BeforeAddressSaveObserver.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Observer;
  7. use Magento\Customer\Helper\Address as HelperAddress;
  8. use Magento\Customer\Model\Address\AbstractAddress;
  9. use Magento\Framework\Registry;
  10. use Magento\Framework\Event\ObserverInterface;
  11. use Magento\Customer\Model\Address;
  12. /**
  13. * Customer Observer Model
  14. */
  15. class BeforeAddressSaveObserver implements ObserverInterface
  16. {
  17. /**
  18. * VAT ID validation currently saved address flag
  19. */
  20. const VIV_CURRENTLY_SAVED_ADDRESS = 'currently_saved_address';
  21. /**
  22. * @var HelperAddress
  23. */
  24. protected $_customerAddress;
  25. /**
  26. * @var Registry
  27. */
  28. protected $_coreRegistry;
  29. /**
  30. * @param HelperAddress $customerAddress
  31. * @param Registry $coreRegistry
  32. */
  33. public function __construct(
  34. HelperAddress $customerAddress,
  35. Registry $coreRegistry
  36. ) {
  37. $this->_customerAddress = $customerAddress;
  38. $this->_coreRegistry = $coreRegistry;
  39. }
  40. /**
  41. * Address before save event handler
  42. *
  43. * @param \Magento\Framework\Event\Observer $observer
  44. * @return void
  45. */
  46. public function execute(\Magento\Framework\Event\Observer $observer)
  47. {
  48. if ($this->_coreRegistry->registry(self::VIV_CURRENTLY_SAVED_ADDRESS)) {
  49. $this->_coreRegistry->unregister(self::VIV_CURRENTLY_SAVED_ADDRESS);
  50. }
  51. /** @var $customerAddress Address */
  52. $customerAddress = $observer->getCustomerAddress();
  53. if ($customerAddress->getId()) {
  54. $this->_coreRegistry->register(self::VIV_CURRENTLY_SAVED_ADDRESS, $customerAddress->getId());
  55. } else {
  56. $configAddressType = $this->_customerAddress->getTaxCalculationAddressType();
  57. $forceProcess = $configAddressType == AbstractAddress::TYPE_SHIPPING
  58. ? $customerAddress->getIsDefaultShipping()
  59. : $customerAddress->getIsDefaultBilling();
  60. if ($forceProcess) {
  61. $customerAddress->setForceProcess(true);
  62. } else {
  63. $this->_coreRegistry->register(self::VIV_CURRENTLY_SAVED_ADDRESS, 'new_address');
  64. }
  65. }
  66. }
  67. }