Delete.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Address;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Framework\App\Action\HttpPostActionInterface;
  10. /**
  11. * Delete customer address controller action.
  12. */
  13. class Delete extends \Magento\Customer\Controller\Address implements HttpPostActionInterface, HttpGetActionInterface
  14. {
  15. /**
  16. * @inheritdoc
  17. * @return \Magento\Framework\Controller\Result\Redirect
  18. */
  19. public function execute()
  20. {
  21. $addressId = $this->getRequest()->getParam('id', false);
  22. if ($addressId && $this->_formKeyValidator->validate($this->getRequest())) {
  23. try {
  24. $address = $this->_addressRepository->getById($addressId);
  25. if ($address->getCustomerId() === $this->_getSession()->getCustomerId()) {
  26. $this->_addressRepository->deleteById($addressId);
  27. $this->messageManager->addSuccess(__('You deleted the address.'));
  28. } else {
  29. $this->messageManager->addError(__('We can\'t delete the address right now.'));
  30. }
  31. } catch (\Exception $other) {
  32. $this->messageManager->addException($other, __('We can\'t delete the address right now.'));
  33. }
  34. }
  35. return $this->resultRedirectFactory->create()->setPath('*/*/index');
  36. }
  37. }