0

I want to do some function in my custom attribute on saving in customer/account/edit,my idea is 1-rewrite the editPostAction() or 2- create an observer on this saving event.

So i rewritted the editPostAction() then i get the param but i dont know how to implemente my function on saving.

My function :

Mage::getModel('custom_telephone/telephone')->validatePhoneNumber()

customer/account/edit:

<li>
   <label for="custom_telephone" class="required"><em>*</em><?php echo $this->__('Phone number') ?></label>
   <div class="input-box">
       <input type="text" name="custom_telephone" id="custom_telephone" value="<?php echo Mage::getModel('custom_telephone/telephone')->removePrefixePhoneNumber($this->escapeHtml($this->getCustomer()->getCustomTelephone())) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Phone number')) ?>" class="input-text validate-number validate-length maximum-length-10 minimum-length-10 required-entry" />
   </div>
</li>

editPostAction() :

public function editPostAction()
    {
        if (!$this->_validateFormKey()) {
            return $this->_redirect('*/*/edit');
        }

        if ($this->getRequest()->isPost()) {
            Mage::log('Start postAction');
            /** @var $customer Mage_Customer_Model_Customer */
            $customer = $this->_getSession()->getCustomer();

            /** @var $customerForm Mage_Customer_Model_Form */
            $customerForm = $this->_getModel('customer/form');
            $customerForm->setFormCode('customer_account_edit')
                ->setEntity($customer);

            $customerData = $customerForm->extractData($this->getRequest());

            $errors = array();
            $customerErrors = $customerForm->validateData($customerData);
            if ($customerErrors !== true) {
                $errors = array_merge($customerErrors, $errors);
            } else {
                $customerForm->compactData($customerData);
                $errors = array();

                // If password change was requested then add it to common validation scheme
                if ($this->getRequest()->getParam('change_password')) {
                    $currPass   = $this->getRequest()->getPost('current_password');
                    $newPass    = $this->getRequest()->getPost('password');
                    $confPass   = $this->getRequest()->getPost('confirmation');

                    $oldPass = $this->_getSession()->getCustomer()->getPasswordHash();
                    if ( $this->_getHelper('core/string')->strpos($oldPass, ':')) {
                        list($_salt, $salt) = explode(':', $oldPass);
                    } else {
                        $salt = false;
                    }
                    if ($customer->hashPassword($currPass, $salt) == $oldPass) {
                        if (strlen($newPass)) {
                            /**
                             * Set entered password and its confirmation - they
                             * will be validated later to match each other and be of right length
                             */
                            $customer->setPassword($newPass);
                            $customer->setPasswordConfirmation($confPass);
                        } else {
                            $errors[] = $this->__('New password field cannot be empty.');
                        }
                    } else {
                        $errors[] = $this->__('Invalid current password');
                    }
                }
                //************ my add *************
                $tel = $this->getRequest()->getPost('custom_telephone');
                $telTransformed = Mage::getModel('custom_telephone/telephone')->validatePhoneNumber($this->getRequest()->getPost('custom_telephone'));
                if($this->getRequest()->getPost('custom_telephone')) {
                    $tel = $telTransformed;
                    Mage::log($tel);
                }
                //*********** End my add ***********                    

                // Validate account and compose list of errors if any
                $customerErrors = $customer->validate();
                if (is_array($customerErrors)) {
                    $errors = array_merge($errors, $customerErrors);
                }
            }

            if (!empty($errors)) {
                $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
                foreach ($errors as $message) {
                    $this->_getSession()->addError($message);
                }
                $this->_redirect('*/*/edit');
                return $this;
            }

            try {
                $customer->cleanPasswordsValidationData();
                $customer->save();
                $this->_getSession()->setCustomer($customer)
                    ->addSuccess($this->__('The account information has been saved.'));

                $this->_redirect('customer/account');
                return;
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->setCustomerFormData($this->getRequest()->getPost())
                    ->addError($e->getMessage());
            } catch (Exception $e) {
                $this->_getSession()->setCustomerFormData($this->getRequest()->getPost())
                    ->addException($e, $this->__('Cannot save the customer.'));
            }
        }

        $this->_redirect('*/*/edit');
    }
4
  • Please explain what error you are facing. BTW, your field name is custom_telephone and you are getting value for wlc_telephone in your code. Commented Mar 7, 2017 at 12:52
  • Sorry, i corrected it it's custom_telephone, i haven't error , my 'Mage::log' get well the value of my attribute but like i have explained it in the question, i want to save the value of custom_telephone with adding my function, exemple the user enter the phone number like this 0500000000 and my function get this number and save it like this +33500000000 Commented Mar 7, 2017 at 13:05
  • did you create a customer attribute to save custom_telephone or saving it to custom table? Commented Mar 7, 2017 at 13:11
  • No, in customer attribute Commented Mar 7, 2017 at 13:12

1 Answer 1

1

To save telephone number in customer attribute,

$tel = $this->getRequest()->getPost('custom_telephone');
$telTransformed = Mage::getModel('custom_telephone/telephone')->validatePhoneNumber($this->getRequest()->getPost('custom_telephone'));
if($this->getRequest()->getPost('custom_telephone')) {
    $tel = $telTransformed;
    $customer->setCustomTelephone($tel);
    Mage::log($tel);
}else{
    $customer->setCustomTelephone(""); // If customer want to remove phone number
}
3
  • im trying it @Jaimin Sutariya Commented Mar 7, 2017 at 13:34
  • just for information this event should work "eav_entity_attribute_save_befor" ? Commented Mar 7, 2017 at 13:42
  • No, the eav_entity_attribute_save_before event is related to when you save an attribute in admin panel in Catalog > Manage Attribute section Commented Mar 7, 2017 at 13:47

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.