Change condition to add new address if no address save in customer My address book
-
02-03-2021 - |
Question
I want to change the condition that redirect you to customer/address/new if you want to see your address book. I just want to open the address book empty without force users to add a new address in order to see the page. But can not find where change that condition in my files.
Solution
You can try the steps below.
Assume you are using a custom module names "Company_MyModule"
Step 1)
Create di.xml under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc
File : YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Controller\Address\Index" type="Company\MyModule\Controller\Address\Index" />
</config>
step 2)
Create the controller file Index.php under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Controller/Address
File : YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Controller/Address/index.php
<?php
namespace Company\MyModule\Controller\Address;
class Index extends \Magento\Customer\Controller\Address\Index
{
public function execute()
{
parent::execute();
$addresses = $this->customerRepository->getById($this->_getSession()->getCustomerId())->getAddresses();
//if (count($addresses)) {
/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$block = $resultPage->getLayout()->getBlock('address_book');
if ($block) {
$block->setRefererUrl($this->_redirect->getRefererUrl());
}
return $resultPage;
//}
}
}
step 3)
Run Di compile
sudo php bin/magento setup:di:compile