1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?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\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Customer\Api\AddressRepositoryInterface;
use Psr\Log\LoggerInterface;
/**
* Button for deletion of customer address in admin
*/
class Delete 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 JsonFactory
*/
private $resultJsonFactory;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param Action\Context $context
* @param AddressRepositoryInterface $addressRepository
* @param JsonFactory $resultJsonFactory
* @param LoggerInterface $logger
*/
public function __construct(
Action\Context $context,
AddressRepositoryInterface $addressRepository,
JsonFactory $resultJsonFactory,
LoggerInterface $logger
) {
parent::__construct($context);
$this->addressRepository = $addressRepository;
$this->resultJsonFactory = $resultJsonFactory;
$this->logger = $logger;
}
/**
* Delete customer address action
*
* @return Json
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(): Json
{
$customerId = $this->getRequest()->getParam('parent_id', false);
$addressId = $this->getRequest()->getParam('id', false);
$error = false;
$message = '';
if ($addressId && $this->addressRepository->getById($addressId)->getCustomerId() === $customerId) {
try {
$this->addressRepository->deleteById($addressId);
$message = __('You deleted the address.');
} catch (\Exception $e) {
$error = true;
$message = __('We can\'t delete the address right now.');
$this->logger->critical($e);
}
}
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData(
[
'message' => $message,
'error' => $error,
]
);
return $resultJson;
}
}