Shipping.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Customer\Attribute\Backend;
  7. /**
  8. * Customer default shipping address backend
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Shipping extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
  13. {
  14. /**
  15. * @param \Magento\Framework\DataObject $object
  16. * @return void
  17. */
  18. public function beforeSave($object)
  19. {
  20. $defaultShipping = $object->getDefaultShipping();
  21. if ($defaultShipping === null) {
  22. $object->unsetDefaultShipping();
  23. }
  24. }
  25. /**
  26. * @param \Magento\Framework\DataObject $object
  27. * @return void
  28. */
  29. public function afterSave($object)
  30. {
  31. if ($defaultShipping = $object->getDefaultShipping()) {
  32. $addressId = false;
  33. /**
  34. * post_index set in customer save action for address
  35. * this is $_POST array index for address
  36. */
  37. foreach ($object->getAddresses() as $address) {
  38. if ($address->getPostIndex() == $defaultShipping) {
  39. $addressId = $address->getId();
  40. }
  41. }
  42. if ($addressId) {
  43. $object->setDefaultShipping($addressId);
  44. $this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getAttributeCode());
  45. }
  46. }
  47. }
  48. }