1

In My Account > Address Book. We have Default address & Company Address section. In Company address, telephone is getting saved as (4-03)9038-383. I want to save it in numbers only i.e. 4039038383

Can someone suggest me how to do the same using plugin.

Thanks, Rashi

1
  • You want to save telephone filed value while save the company on backend ? Correct ? Commented Dec 14, 2022 at 11:26

1 Answer 1

0

Create an event in your custom module app/code/Vendor/Module/etc/events.xml

<event name="customer_address_save_before">
     <observer name="telephone_no_customer_address_model_save_before" instance="Vendor\Module\Observer\Customer\ModelSaveBefore"/>
</event>

Create ModelSaveBefore.php inside Vendor\Module\Observer\Customer

<?php
namespace Vendor\Module\Observer\Customer;

use Magento\Customer\Model\Customer;
use Magento\Customer\Model\CustomerFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ModelSaveBefore implements ObserverInterface
{
    protected $customerFactory;

    public function __construct(CustomerFactory $customerFactory)
    {
        $this->customerFactory = $customerFactory;
    }
    
    public function execute(Observer $observer)
    {
        $customerAddress = $observer->getCustomerAddress();
        $telephone = $customerAddress->getTelephone();
        $customerAddress->setTelephone(preg_replace("/[^a-zA-Z0-9]/", "", $telephone));
    }
}

Run commands accordingly and get your work done

UPDATE: Using Plugin

Add in etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Vendor\Module\Plugin\Customer\ModelSaveBefore">
        <plugin name="telephone_no_customer_address_model_save_before" type="Magento\Customer\Observer\BeforeAddressSaveObserver" sortOrder="10" disabled="false"  />
    </type>
</config>

Create Vendor\Module\Plugin\Customer\ModelSaveBefore.php

And try below code:

<?php
namespace Vendor\Module\Plugin\Customer;

use Magento\Customer\Observer\BeforeAddressSaveObserver

class ModelSaveBefore 
{
    public function beforeExecute(BeforeAddressSaveObserver $subject, $result)
    {
        $customerAddress = $result->getCustomerAddress();
        $telephone = $customerAddress->getTelephone();
        $customerAddress->setTelephone(preg_replace("/[^a-zA-Z0-9]/", "", $telephone));
        return $result;
    }
}
7
  • Hi Arun, Thanks for the input but I want to utilise plugin only for this action. Can you guide me on this using plugin? Also, this is required for company address only. Commented Dec 13, 2022 at 7:30
  • can you please look into my query asap? Commented Dec 13, 2022 at 14:00
  • Please check the updated ans Commented Dec 14, 2022 at 7:36
  • Arun- This is for customer but I want to do the same for Company only i.e. when from the backend I will create a new company & click on save. At that time,I want to utilise this logic before save Commented Dec 14, 2022 at 7:48
  • @RashiGoyal Magento have only two options in admin for address by default. Can you please share the screenshot? Commented Dec 14, 2022 at 8:13

Your Answer

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

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