state or province

In magento there are some validations which are specified in Mage_Customer_Model_Address_Abstract class and in some cases you might need to change or remove these validations. Overriding abstract class can be tricky in Magento and not recommended as well because the abstract class can be used by quite few classes so impact of changing abstract class is quite high.

But the good news is that you can do this without overwriting abstract class, today we are going to see how can we achieve this.

See below our step by step implemetation -:

Step 1

Create a module under local folder i.e. app/code/local/Scommerce/Validation

Step 2

Register your module by creating Scommerce_Validation.xml under etc/modules directory by adding the following content -:

<Scommerce_Validation> <active>true</active> <codePool>local</codePool>
<Scommerce_Validation>

Step 3

Now create a config.xml under your module directory i.e. app/code/local/Scommerce/Validation/etc

<?xml version="1.0"?>
<config> <modules> <Scommerce_Validation> <version>0.0.1</version> </Scommerce_Validation> </modules> <global> <models> <scommerce_validation> <class>Scommerce_Validation_Model</class> </scommerce_validation> <customer> <rewrite> <address>Scommerce_Validation_Model_Address</address> </rewrite> </customer> </models> </global>
</config>

Step 4

Now create Address.php inside the model directory of your module i.e. app/code/local/Scommerce/Validation/Model

<?php class Scommerce_Validation_Model_Address extends Mage_Customer_Model_Address
{ public function validate() { $errors = parent::validate(); $helper = Mage::helper('customer'); $stateError = $helper->__('Please enter the state/province.'); if(in_array($stateError, $errors)) { unset($errors[array_search($stateError, $errors)]); } if (empty($errors)) { return true; } return $errors; }
}

You might have noticed that instead of overriding abstract class, we have overridden address class which is actually calling validate function of the abstract class. This is the most efficient way of dealing with abstract classes in magento. Always try not to override abstract class and find out the child classes which are calling abstract class if you can get around the way we did it above.

Hope you like our implementation but if you have better way than this then please don’t hesistate to share with us. Thanks.

Similar Posts