DefaultBillingAddress.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Controller\Adminhtml\Address;
  9. use Magento\Backend\App\Action;
  10. use Magento\Customer\Api\Data\AddressInterface;
  11. use Magento\Customer\Api\AddressRepositoryInterface;
  12. use Magento\Framework\App\Action\HttpPostActionInterface;
  13. use Magento\Framework\Controller\Result\Json;
  14. use Magento\Framework\Controller\Result\JsonFactory;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17. * Class to process set default billing address action
  18. */
  19. class DefaultBillingAddress extends Action implements HttpPostActionInterface
  20. {
  21. /**
  22. * Authorization level of a basic admin session
  23. *
  24. * @see _isAllowed()
  25. */
  26. public const ADMIN_RESOURCE = 'Magento_Customer::manage';
  27. /**
  28. * @var AddressRepositoryInterface
  29. */
  30. private $addressRepository;
  31. /**
  32. * @var LoggerInterface
  33. */
  34. private $logger;
  35. /**
  36. * @var JsonFactory
  37. */
  38. private $resultJsonFactory;
  39. /**
  40. * @param Action\Context $context
  41. * @param AddressRepositoryInterface $addressRepository
  42. * @param LoggerInterface $logger
  43. * @param JsonFactory $resultJsonFactory
  44. */
  45. public function __construct(
  46. Action\Context $context,
  47. AddressRepositoryInterface $addressRepository,
  48. LoggerInterface $logger,
  49. JsonFactory $resultJsonFactory
  50. ) {
  51. parent::__construct($context);
  52. $this->addressRepository = $addressRepository;
  53. $this->logger = $logger;
  54. $this->resultJsonFactory = $resultJsonFactory;
  55. }
  56. /**
  57. * Execute action to set customer default billing address
  58. *
  59. * @return Json
  60. */
  61. public function execute(): Json
  62. {
  63. $customerId = $this->getRequest()->getParam('parent_id', false);
  64. $addressId = $this->getRequest()->getParam('id', false);
  65. $error = true;
  66. $message = __('There is no address id in setting default billing address.');
  67. if ($addressId) {
  68. try {
  69. $address = $this->addressRepository->getById($addressId)->setCustomerId($customerId);
  70. $this->setAddressAsDefault($address);
  71. $this->addressRepository->save($address);
  72. $message = __('Default billing address has been changed.');
  73. $error = false;
  74. } catch (\Exception $e) {
  75. $message = __('We can\'t change default billing address right now.');
  76. $this->logger->critical($e);
  77. }
  78. }
  79. $resultJson = $this->resultJsonFactory->create();
  80. $resultJson->setData(
  81. [
  82. 'message' => $message,
  83. 'error' => $error,
  84. ]
  85. );
  86. return $resultJson;
  87. }
  88. /**
  89. * Set address as default billing address
  90. *
  91. * @param AddressInterface $address
  92. * @return void
  93. */
  94. private function setAddressAsDefault(AddressInterface $address): void
  95. {
  96. $address->setIsDefaultBilling(true);
  97. }
  98. }