2

Can someone please tell me how can change customer name format globally?

I want to show customer name as Last name First name instead of First name then Last name.

I ask this because I'm doing an e-commerce site in Japanese.

2 Answers 2

2

in magento full name are comming from addNameToSelect() function of class Mage_Customer_Model_Resource_Customer_Collection and getName() function of class Mage_Customer_Model_Customer.You need change there by rewrite classes.

  <global>
    <models>
      <magento70422>
        <class>Stackexchange_Magento70422_Model</class>
      </magento70422>
            <customer_resource>
                <rewrite>
                    <customer_collection>Stackexchange_Magento70422_Model_Customer_Resource_Customer_Collection</customer_collection>
                </rewrite>
            </customer_resource>
            <customer>
                <rewrite>
                    <customer>Stackexchange_Magento70422_Model_Customer_Customer</customer>
                </rewrite>
            </customer>
    </models>
  </global>

Rewrite class of Mage_Customer_Model_Customer

<?php
class Stackexchange_Magento70422_Model_Customer_Customer extends Mage_Customer_Model_Customer
{

   public function getName()
    {
        $name = '';
        $config = Mage::getSingleton('eav/config');
        if ($config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
            $name .= $this->getPrefix() . ' ';
        }
        //$name .= $this->getFirstname();
    $name .=  $this->getLastname();
        if ($config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
            $name .= ' ' . $this->getMiddlename();
        }
        //$name .=  ' ' . $this->getLastname();
    $name .= ' ' . $this->getFirstname();   
        if ($config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
            $name .= ' ' . $this->getSuffix();
        }
        return $name;
    }
}

Rewrite resource collection class of Mage_Customer_Model_Resource_Customer_Collection is below:

<?php
class Stackexchange_Magento70422_Model_Customer_Resource_Customer_Collection extends Mage_Customer_Model_Resource_Customer_Collection
{
public function addNameToSelect()
    {
        $fields = array();
        $customerAccount = Mage::getConfig()->getFieldset('customer_account');
        foreach ($customerAccount as $code => $node) {
            if ($node->is('name')) {
                $fields[$code] = $code;
            }
        }

        $adapter = $this->getConnection();
        $concatenate = array();
        if (isset($fields['prefix'])) {
            $concatenate[] = $adapter->getCheckSql(
                '{{prefix}} IS NOT NULL AND {{prefix}} != \'\'',
                $adapter->getConcatSql(array('LTRIM(RTRIM({{prefix}}))', '\' \'')),
                '\'\'');
        }
       // $concatenate[] = 'LTRIM(RTRIM({{firstname}}))';
        $concatenate[] = 'LTRIM(RTRIM({{lastname}}))';

        $concatenate[] = '\' \'';
        if (isset($fields['middlename'])) {
            $concatenate[] = $adapter->getCheckSql(
                '{{middlename}} IS NOT NULL AND {{middlename}} != \'\'',
                $adapter->getConcatSql(array('LTRIM(RTRIM({{middlename}}))', '\' \'')),
                '\'\'');
        }
       // $concatenate[] = 'LTRIM(RTRIM({{lastname}}))';
    $concatenate[] = 'LTRIM(RTRIM({{firstname}}))'
        if (isset($fields['suffix'])) {
            $concatenate[] = $adapter
                    ->getCheckSql('{{suffix}} IS NOT NULL AND {{suffix}} != \'\'',
                $adapter->getConcatSql(array('\' \'', 'LTRIM(RTRIM({{suffix}}))')),
                '\'\'');
        }

        $nameExpr = $adapter->getConcatSql($concatenate);

        $this->addExpressionAttributeToSelect('name', $nameExpr, $fields);

        return $this;
    }
}
3
  • Thank you for your answer. The "Bill to Name" field in Sales -> Orders table is still in the original format (First name then Last name). I'm wondering why magento get customer name functions are placed in so many places. Commented Jun 9, 2015 at 4:43
  • you need to change all position
    – Amit Bera
    Commented Jun 9, 2015 at 7:34
  • god bless me... Anyway, thank you for the information! Commented Jun 9, 2015 at 10:36
0

In \app\code\core\Mage\Customer\Model\Customer.php there is a method called getName() that looks like this:

public function getName()
    {
        $name = '';
        $config = Mage::getSingleton('eav/config');
        if ($config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
            $name .= $this->getPrefix() . ' ';
        }
        $name .= $this->getFirstname();
        if ($config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
            $name .= ' ' . $this->getMiddlename();
        }
        $name .=  ' ' . $this->getLastname();
        if ($config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
            $name .= ' ' . $this->getSuffix();
        }
        return $name;
    }

Change it by simply inversing getFirstname with getLastname. To change this without modifying the core file, you can copy this file to the local code pool, or create your own module and rewrite this method in it.

1
  • Isn't this better handled by Mage_Directory? Or is name formatting not in there? Been a while since I looked at it.
    – user4351
    Commented Jun 9, 2015 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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