Magento 2 : Delete address from after login from address book
-
17-12-2020 - |
Question
I want to delete the address which I have saved earlier from here but in vendor-> Magento_Customer\templates\address in addressbook file we have only edit address action but not delete action so please help me any help will be appreciated.
Here is the code for delete anchor tag->
<div class="item actions">
<a class="action edit" href="<?php /* @escapeNotVerified */ echo $block->getUrl('customer/address/edit', ['id' => $_address->getId()]) ?>"><span><?php /* @escapeNotVerified */ echo __('Edit Address') ?></span></a><a class="action delete" href="#" role="delete-address" data-address="<?php /* @escapeNotVerified */ echo $_address->getId() ?>"><span><?php /* ## Heading ##@escapeNotVerified */ echo __('Delete Address') ?></span></a>
</div>
Solution
This is because Magento allows deleting the address when having one more addresses. We can add one more address and will see the delete button.
If we're really want to delete the address, we can base on the script:
vendor/magento/module-customer/view/frontend/templates/address/book.phtml
<script type="text/x-magento-init">
{
".page-main": {
"address": {
"deleteAddress": "li.item a[role='delete-address']",
"deleteUrlPrefix": "<?= $block->escapeJs($block->escapeUrl($block->getDeleteUrl())) ?>id/",
"addAddress": "button[role='add-address']",
"addAddressLocation": "<?= $block->escapeJs($block->escapeUrl($block->getAddAddressUrl())) ?>"
}
}
}
</script>
[EDIT]
We can add the delete button:
<form action="<?= $block->escapeUrl($block->getUrl('customer/address/delete', ['id' => $_pAddsses])) ?>">
<?= $block->getBlockHtml('formkey') ?>
<button type="submit" class="action delete">
<span><?= $block->escapeHtml(__('Delete Address')) ?></span>
</button>
</form>
$_pAddsses
is the id of address.
We need to add the form key to form because vendor/magento/module-customer/Controller/Address/Delete.php
will check.