1

I am not able to delete the additional address from the address book.

Code for script

<script type="text/x-magento-init">
{
    ".page-main": {
        "address": {
            "deleteAddress": "div.mobile-address-edit a[role='delete-address']",
            "deleteUrlPrefix": "<?= $block->escapeJs($block->escapeUrl($block->getDeleteUrl())) ?>id/",
            "addAddress": "button[role='add-address']",
            "addAddressLocation": "<?= $block->escapeJs($block->escapeUrl($block->getAddAddressUrl())) ?>"
        }
    }
}

phtml which I changed

 <div class="mobile-address-edit">
                <a class="action edit address-edit" id= "<?= $_address->getId() ?>" data-json="<?php echo htmlentities($json, ENT_QUOTES, 'UTF-8'); ?>" >
                    <span><?= $block->escapeHtml(__('Edit')) ?></span>
                </a>

                <a class="action delete" href="#" role="delete-address" data-address="<?= $block->escapeHtmlAttr($_address->getId()) ?>">
                    <span><?= $block->escapeHtml(__('Delete ')) ?></span>
                </a>
   </div>

It gives me this error.

enter image description here

I don't know what is the issue. I am not able to figure out

0

1 Answer 1

0

Hi I solved it by writing my own controller

<?php


 namespace YX\Catalog\Controller\Index;


use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;

class DeleteAddress extends \Magento\Framework\App\Action\Action
{

  protected $_addressRepository;
  protected $resultJsonFactory;

/**
 * Execute action based on request and return result
 *
 * Note: Request will be added as operation argument in future
 *
 * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
 * @throws \Magento\Framework\Exception\NotFoundException
 */


public function __construct(
    \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
    \Magento\Framework\App\Action\Context $context


)
{
    $this->_addressRepository = $addressRepository;
    $this->resultJsonFactory = $resultJsonFactory;
    parent::__construct($context);
}


public function execute()
{
    // TODO: Implement execute() method.

    $request = $this->getRequest();
    $addressId = $request->getParam('address_id');
    // $addressId = $this->getRequest()->getPost("address_id");

    $returnArray = [];

    try{

        $this->_addressRepository->deleteById($addressId);
        return  $this->resultJsonFactory->create()->setData(['success' => true,'message' => $this->messageManager->addSuccess("Successfully deleted customer address")]);

    } catch(\Exception $e) {

        $delete_error  = $this->messageManager->addError($e->getMessage());
        return  $this->resultJsonFactory->create()->setData(['success' => false,'message' => $this->messageManager->addError('Address cannot be deleted')]);

    }
  }
}

And calling this controller using ajax call.

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.