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.
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;
}
}
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.