0

I don't know how to implement this task. I need to Save the deleting customer book address id in customer custom attribute the customer on Magento2. Could you please help me?

  • please describe more what you want to achive ? – Msquare Mar 30 at 9:35
  • where to save customer address id ? – Msquare Mar 30 at 9:41
  • Hi@Msquare, save the address id of the customer on the customer level – Dileep Kumar Mar 30 at 9:50
  • Customer Level ? please describe me – Msquare Mar 30 at 9:53
  • Hi@Msquare, I want to delete the customer address in frontend and backend and that customer address id saves to the customer entity of the customer. for example, I want two customer ids and I delete customer2 and that save to the customer entity of the customer. Could you please help me? – Dileep Kumar Mar 30 at 11:21
1

Add this two files to your module

app/code/VendoreName/ModuleName/view/frontend/layout

customer_address_index.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Customer\Block\Address\Book" name="address_book" template="VendoreName_ModuleName::address/book.phtml" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

app/code/VendoreName/ModuleName/view/frontend/templates/address

book.phtml

<div class="block block-addresses-default">
    <div class="block-title"><strong><?= $block->escapeHtml(__('Default Addresses')) ?></strong></div>
    <div class="block-content">
        <?php if ($_pAddsses = $block->getDefaultBilling()) : ?>
            <div class="box box-address-billing">
                <strong class="box-title">
                    <span><?= $block->escapeHtml(__('Default Billing Address')) ?></span>
                </strong>
                <div class="box-content">
                    <address>
                        <?= $block->getAddressHtml($block->getAddressById($_pAddsses)) ?>
                    </address>
                </div>
                <div class="box-actions">
                    <a class="action edit" href="<?= $block->escapeUrl($block->getAddressEditUrl($_pAddsses)) ?>">
                        <span><?= $block->escapeHtml(__('Change Billing Address')) ?></span>
                    </a>
                </div>
                <div class="primary">
                 <table class="data table table-additional-addresses-items history" border="0">               
                    <tbody>
                        <tr>
                            <td data-th="Actions" class="col actions" style="border: none;">
                                <a class="action delete" href="#" role="delete-address" data-address="<?php echo $_pAddsses; ?>"><button>Delete</button></a>
                            </td>
                        </tr>
                    </tbody>
                </table>
                </div>
            </div>
        <?php else : ?>
            <div class="box box-billing-address">
                <strong class="box-title"><span><?= $block->escapeHtml(__('Default Billing Address')) ?></span></strong>
                <div class="box-content">
                    <p><?= $block->escapeHtml(__('You have no default billing address in your address book.')) ?></p>
                </div>
            </div>
        <?php endif ?>

        <?php if ($_pAddsses = $block->getDefaultShipping()) : ?>
            <div class="box box-address-shipping">
                <strong class="box-title">
                    <span><?= $block->escapeHtml(__('Default Shipping Address')) ?></span>
                </strong>
                <div class="box-content">
                    <address>
                        <?= $block->getAddressHtml($block->getAddressById($_pAddsses)) ?>
                    </address>
                </div>
                <div class="box-actions">
                    <a class="action edit" href="<?= $block->escapeUrl($block->getAddressEditUrl($_pAddsses)) ?>">
                        <span><?= $block->escapeHtml(__('Change Shipping Address')) ?></span>
                    </a>
                </div>
                <div class="primary">
                 <table class="data table table-additional-addresses-items history" border="0">               
                    <tbody>
                        <tr>
                            <td data-th="Actions" class="col actions" style="border: none;" >
                                <a class="action delete" href="#" role="delete-address" data-address="<?php echo $_pAddsses; ?>"><button>Delete</button></a>
                            </td>
                        </tr>
                    </tbody>
                </table>
                </div>
            </div>
        <?php else : ?>
            <div class="box box-shipping-address">
                <strong class="box-title"><span><?= $block->escapeHtml(__('Default Shipping Address')) ?></span></strong>
                <div class="box-content">
                    <p><?= $block->escapeHtml(__('You have no default shipping address in your address book.')) ?></p>
                </div>
            </div>
        <?php endif ?>
    </div>
</div>

enter image description here

Update

<?php

namespace VendoreName\ModuleName\Controller\Address;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;

class ExtendDelete extends \Magento\Customer\Controller\Address implements HttpPostActionInterface, HttpGetActionInterface
{

    public function execute()
    {
        echo "<pre>";
        $addressId = $this->getRequest()->getParam('id', false);

        if ($addressId && $this->_formKeyValidator->validate($this->getRequest())) {
            try {
                $address = $this->_addressRepository->getById($addressId);
                if ($address->getCustomerId() === $this->_getSession()->getCustomerId()) {
                    //  $this->_addressRepository->deleteById($addressId);

                    // save your deleted id here 

                    $customer_id = $this->_getSession()->getCustomerId();
                    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

                    $customerRepository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');
                    $customer = $customerRepository->getById($customer_id);


                    if(empty($customer->getCustomAttribute("address_book")) ){
                        $add_id = $addressId;
                    } else {
                        $add_id = $customer->getCustomAttribute("address_book")->getValue().",".$addressId;
                    }

                    $add_id = ltrim($add_id, ',');

                    $customer->setCustomAttribute("address_book",$add_id);
                    $customerRepository->save($customer);
                    exit;

                    $this->messageManager->addSuccess(__('You deleted the address.'));
                } else {
                    $this->messageManager->addError(__('We can\'t delete the address right now.'));
                }
            } catch (\Exception $other) {
                $this->messageManager->addException($other, __('We can\'t delete the address right now.'));
            }
        }
        return $this->resultRedirectFactory->create()->setPath('*/*/index');
    }
}

I Hope This Helps You.

| improve this answer | |
  • Hi@Msquare, Thanks for posting the answer actually my requirement is creating the customer custom attribute and save the deleting address id in that attribute. I do not change the core files on how to override the core files. delete logic in vendor/magento/module-customer/Controller/Address/Delete.php please help me? – Dileep Kumar Apr 1 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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