Add function in a custom attribute on save - Magento 1.9
-
07-10-2020 - |
题
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');
}
解决方案
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
}