2

When a customer make an order, in the shipping and billing address appears the extended Province name (for example "Lecce"). Is it possible to show the Region code "LE" instead of the Region name "Lecce"?

Here there is the DB structure:

Country ID > IT
Code (Region code) > LE
Default_name (Region name) > Lecce

I don't want to modify directly this table "directory_country_region" in the DB.

  • Where do you want to show this ? In admin side or in customer account section ? – Rajeev K Tomy Aug 3 '16 at 12:10
0

You can do this at-least in two ways.

1. Observer way - Recommending Way

If you want to see this change in admin side, then you can observe to the event customer_address_format and define following observer method within it.

public function changeRegionText(Varien_Event_Observer $observer)
{
    $address = $observer->getAddress();
    //makes sure change apply to only billing and shipping address types
    if ($address->getAddressType()=='shipping' || $address->getAddressType()=='billing') {
        $code = $address->getRegionCode();
        $address->setRegion($code);
    }

    return $this;
}

As you can see, I am just grabbing region code from address entity and then forcefully set it as region.

You can also listen to a 'broader event' sales_order_address_collection_load_after and do the same thing by looping through the collection. ie.

public function changeRegionText(Varien_Event_Observer $observer)
{
    $collection = $observer->getOrderAddressCollection();
    foreach ($collection as $address) {

        //makes sure change apply to only billing and shipping address types
        if ($address->getAddressType()=='shipping' || $address->getAddressType()=='billing') {
            $code = $address->getRegionCode();
            $address->setRegion($code);
        }
    }

    return $this;
}

This broader way allows to give your change permanently everywhere in your instance.

2. Rewrite Way - Easy Nasty Way

If you want to do the change in admin side, then you need to rewrite block class Mage_Customer_Block_Address_Renderer_Default through your module and customize render() method.

This is because this class what actually responsible for rendering billing & shipping address in admin side. You need to replace below code line

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

with

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

which you can find inside render() method.

  • which files have I to modify? – stsnow Aug 3 '16 at 12:41
  • you need to create a module in this case to do both case 1 and 2. I – Rajeev K Tomy Aug 3 '16 at 13:25
  • What should be the file config.xml? – stsnow Aug 3 '16 at 14:53
  • @stsnow In order to create a module, multiple files are involving in it. You should know how to create a stand alone module in Magento in order to understand and implement idea which I shared above. – Rajeev K Tomy Aug 4 '16 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.