I recently worked on a store where I needed to make the last name field optional at both checkout and account levels.
I found a quick guide here: http://magento.stackexchange.com/questions/13878/how-to-make-last-name-optional-in-magento-1-8 – but this didn’t include all the steps I needed to take for the Magento 1.9 instance I had, so I wanted to note down all the steps I needed to take for future reference!
1. Remove the classes for JS validation from your templates.
customer/widget/name.phtml
Remove class="required"
from the label relating to the ‘lastname’ field at line 80
Remove < ?php echo $this->helper('customer/address')
from the input relating to ‘lastname’ at line 82
->getAttributeValidationClass('lastname') ?>
2. Copy your core files from:
/app/code/core/Mage/customer/Model/Address/Abstract.php
/app/code/core/Mage/customer/Model/Customer.php
into:
/app/code/local/Mage/customer/Model/Address/Abstract.php
/app/code/local/Mage/customer/Model/Customer.php
And comment out everything related to Zend validation of Lastname.
/app/code/local/Mage/customer/Model/Address/Abstract.php:
1 2 3 4 5 |
/* if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) { $this->addError(Mage::helper('customer')->__('Please enter the last name.')); } */ |
/app/code/local/Mage/customer/Model/Customer.php
1 2 3 4 5 |
/* if (!Zend_Validate::is( trim($this->getLastname()) , 'NotEmpty')) { $errors[] = Mage::helper('customer')->__('The last name cannot be empty.'); } */ |
3. Update the config.xml file for customer
You either need to hack Magento core (no thanks) – or create your own module to patch the config.xml node
/app/code/core/Mage/customer/etc/config.xml
you have to search for:
1 2 3 4 5 6 |
<lastname> <billing>1</billing> <shipping>1</shipping> <required>0</required> <mapped>1</mapped> </lastname> |
4. Update the eav_attribute database table to remove the required flag
UPDATE eav_attribute SET is_required = 0 WHERE attribute_code = 'lastname'
This should affect 2 rows. The customer lastname, and the customer address lastname.
5. Update the validation rules in the customer_eav_attribute table
You will find reference to the customer lastname and customer address lastname. In my instance, these were IDs 7, and 22. As I just wanted to remove the minimum character count (i.e. the validation states there must be at least 1 character in there), I updated them to leave a maximum.
UPDATE customer_eav_attribute SET
validate_rules = 'a:1:{s:15:"max_text_length";i:255;}'
WHERE customer_eav_attribute.attribute_id = 7;
UPDATE customer_eav_attribute SET
validate_rules = 'a:1:{s:15:"max_text_length";i:255;}'
WHERE customer_eav_attribute.attribute_id = 22;
Image Credit: Wood...
Comment or tweet @douglasradburn