1

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>

1 Answer 1

5

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.

14
  • after adding this script what should i add in the href="?" section of delete anchor tag Nov 23, 2017 at 13:53
  • "deleteAddress": "li.item a[role='delete-address']", it's the element selection. Do you want to re-use the js? Nov 23, 2017 at 13:54
  • i would prefer like above edit button so delete directly by taking address Id Nov 23, 2017 at 13:59
  • 1
    I will check and let you know. Nov 23, 2017 at 14:01
  • 1
    @sourabhyadav I updated my answer. Nov 24, 2017 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.