Search code examples
magento

Magento Address Templates Region Code


I'm creating a website in Magento (v.1.9.2.4) and I want to format the addresses using only the region code instead of the whole region name (Ex: Rhode Island to RI)

I know the template is located on Customer Configuration > Address Templates

{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
{{depend company}}{{var company}}
{{/depend}} {{if street1}}{{var street1}}
{{/if}} {{depend street2}}{{var street2}}
{{/depend}} {{depend street3}}{{var street3}}
{{/depend}} {{depend street4}}{{var street4}}
{{/depend}} {{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
{{var country}}
{{depend telephone}}T: {{var telephone}}{{/depend}} {{depend fax}}
F: {{var fax}}{{/depend}} {{depend vat_id}}
VAT: {{var vat_id}}{{/depend}}`

But when I change {{if region}}{{var region}} to {{if regioncode}}{{var regioncode}} or {{if region_code}}{{var region_code}} it does not work.

Does someone can help me with that? I tried to search for a list of fields as well but I couldn't find anything.

Thanks


Solution

  • regioncode is not customer address attribute. Only region and region_id are available.

    {{/if}}{{if region}}{{var region}}

    will show the full region name like California We can override the address block in our local folder and customize the code to show the regioncode like CA.

    Change the customer address template:

    System->Configuration->Customer->Customer Configuration

    and scroll down to Address Templates and edit:

    {{/if}}{{if region}}{{var region}} replace to {{/if}}{{if region_id}}{{var region_id}}

    To override the block copy

    ‘app/code/core/Mage/Customer/Block/Address/Renderer/Default.php’

    into

    ‘app/code/local/Mage/Customer/Block/Address/Renderer/Default.php’

    . Doing only this will let magento read this file from local folder instead of core folder and change render() method as below:

    public function render(Mage_Customer_Model_Address_Abstract $address, $format=null)
        {
            switch ($this->getType()->getCode()) {
                case 'html':
                    $dataFormat = Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_HTML;
                    break;
                case 'pdf':
                    $dataFormat = Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_PDF;
                    break;
                case 'oneline':
                    $dataFormat = Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_ONELINE;
                    break;
                default:
                    $dataFormat = Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_TEXT;
                    break;
            }
    
            $formater   = new Varien_Filter_Template();
            $attributes = Mage::helper('customer/address')->getAttributes();
    
            $data = array();
            foreach ($attributes as $attribute) {
                /* @var $attribute Mage_Customer_Model_Attribute */
                if (!$attribute->getIsVisible()) {
                    continue;
                }
                if ($attribute->getAttributeCode() == 'country_id') {
                    $data['country'] = $address->getCountryModel()->getName();
                } else if ($attribute->getAttributeCode() == 'region') {
                    $data['region'] = Mage::helper('directory')->__($address->getRegion());
                }else if ($attribute->getAttributeCode() == 'region_id') {
    

    $data['region_id'] = Mage::helper('directory')->__($address->getRegionCode());

                } else {
                    $dataModel = Mage_Customer_Model_Attribute_Data::factory($attribute, $address);
                    $value     = $dataModel->outputValue($dataFormat);
                    if ($attribute->getFrontendInput() == 'multiline') {
                        $values    = $dataModel->outputValue(Mage_Customer_Model_Attribute_Data::OUTPUT_FORMAT_ARRAY);
                        // explode lines
                        foreach ($values as $k => $v) {
                            $key = sprintf('%s%d', $attribute->getAttributeCode(), $k + 1);
                            $data[$key] = $v;
                        }
                    }
                    $data[$attribute->getAttributeCode()] = $value;
                }
            }
    
            if ($this->getType()->getHtmlEscape()) {
                foreach ($data as $key => $value) {
                    $data[$key] = $this->escapeHtml($value);
                }
            }
    
            $formater->setVariables($data);
    
            $format = !is_null($format) ? $format : $this->getFormat($address);
    
            return $formater->filter($format);
        }