DeleteRelation.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\ResourceModel\Address;
  7. use Magento\Customer\Api\Data\CustomerInterface;
  8. /**
  9. * Class DeleteRelation
  10. * @package Magento\Customer\Model\ResourceModel\Address
  11. */
  12. class DeleteRelation
  13. {
  14. /**
  15. * Delete relation (billing and shipping) between customer and address
  16. *
  17. * @param \Magento\Framework\Model\AbstractModel $address
  18. * @param \Magento\Customer\Model\Customer $customer
  19. * @return void
  20. */
  21. public function deleteRelation(
  22. \Magento\Framework\Model\AbstractModel $address,
  23. \Magento\Customer\Model\Customer $customer
  24. ) {
  25. $toUpdate = $this->getDataToUpdate($address, $customer);
  26. if (!$address->getIsCustomerSaveTransaction() && !empty($toUpdate)) {
  27. $address->getResource()->getConnection()->update(
  28. $address->getResource()->getTable('customer_entity'),
  29. $toUpdate,
  30. $address->getResource()->getConnection()->quoteInto('entity_id = ?', $customer->getId())
  31. );
  32. }
  33. }
  34. /**
  35. * Return address type (billing or shipping), or null if address is not default
  36. *
  37. * @param \Magento\Customer\Api\Data\AddressInterface $address
  38. * @param \Magento\Customer\Api\Data\CustomerInterface $customer
  39. * @return array
  40. */
  41. private function getDataToUpdate(
  42. \Magento\Framework\Model\AbstractModel $address,
  43. \Magento\Customer\Model\Customer $customer
  44. ) {
  45. $toUpdate = [];
  46. if ($address->getId()) {
  47. if ($customer->getDefaultBilling() == $address->getId()) {
  48. $toUpdate[CustomerInterface::DEFAULT_BILLING] = null;
  49. }
  50. if ($customer->getDefaultShipping() == $address->getId()) {
  51. $toUpdate[CustomerInterface::DEFAULT_SHIPPING] = null;
  52. }
  53. }
  54. return $toUpdate;
  55. }
  56. }