123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- declare(strict_types=1);
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Controller\Adminhtml\Address;
- use Magento\Backend\App\Action;
- use Magento\Customer\Api\Data\AddressInterface;
- use Magento\Customer\Api\AddressRepositoryInterface;
- use Magento\Framework\App\Action\HttpPostActionInterface;
- use Magento\Framework\Controller\Result\Json;
- use Magento\Framework\Controller\Result\JsonFactory;
- use Psr\Log\LoggerInterface;
- /**
- * Class to process set default shipping address action
- */
- class DefaultShippingAddress extends Action implements HttpPostActionInterface
- {
- /**
- * Authorization level of a basic admin session
- *
- * @see _isAllowed()
- */
- public const ADMIN_RESOURCE = 'Magento_Customer::manage';
- /**
- * @var AddressRepositoryInterface
- */
- private $addressRepository;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @var JsonFactory
- */
- private $resultJsonFactory;
- /**
- * @param Action\Context $context
- * @param AddressRepositoryInterface $addressRepository
- * @param LoggerInterface $logger
- * @param JsonFactory $resultJsonFactory
- */
- public function __construct(
- Action\Context $context,
- AddressRepositoryInterface $addressRepository,
- LoggerInterface $logger,
- JsonFactory $resultJsonFactory
- ) {
- parent::__construct($context);
- $this->addressRepository = $addressRepository;
- $this->logger = $logger;
- $this->resultJsonFactory = $resultJsonFactory;
- }
- /**
- * Execute action to set customer default shipping address
- *
- * @return Json
- */
- public function execute(): Json
- {
- $customerId = $this->getRequest()->getParam('parent_id', false);
- $addressId = $this->getRequest()->getParam('id', false);
- $error = true;
- $message = __('There is no address id in setting default shipping address.');
- if ($addressId) {
- try {
- $address = $this->addressRepository->getById($addressId)->setCustomerId($customerId);
- $this->setAddressAsDefault($address);
- $this->addressRepository->save($address);
- $message = __('Default shipping address has been changed.');
- $error = false;
- } catch (\Exception $e) {
- $message = __('We can\'t change default shipping address right now.');
- $this->logger->critical($e);
- }
- }
- $resultJson = $this->resultJsonFactory->create();
- $resultJson->setData(
- [
- 'message' => $message,
- 'error' => $error,
- ]
- );
- return $resultJson;
- }
- /**
- * Set address as default shipping address
- *
- * @param AddressInterface $address
- * @return void
- */
- private function setAddressAsDefault(AddressInterface $address): void
- {
- $address->setIsDefaultShipping(true);
- }
- }
|