
In this article we will see how to delete customer address by address id. It is very simple, you just need to use customer address repository method deleteById, here is the code snippet:
<?php
/**
* Webkul Software.
*
* @category Webkul
* @package Webkul_DeleteCustomer
* @author Webkul
*/
namespace Webkul\DeleteCustomer\Controller\Customer;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
/**
* Delete Customer
*/
class AddressDelete extends Action
{
/**
* $_addressRepository
* @var \Magento\Customer\Api\AddressRepository
*/
protected $_addressRepository;
/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository
) {
$this->_addressRepository = $addressRepository;
parent::__construct($context);
}
/**
* execute delete customer
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
$addressId = $this->getRequest()->getPost("address_id");
$returnArray = [];
try{
$this->_addressRepository->deleteById($addressId);
$this->messageManager->addSuccess("Successfully deleted customer address");
} catch(\Exception $e) {
$this->messageManager->addError($e->getMessage());
}
}
}
The code is very simple and you can use this in your model or helper class you just need to load the customer address repository class in the constructor.
Thanks 🙂

Be the first to comment.