The question:
In admin panel, when I want to search the orders based on customers name, I have to add 2 spaces between the first name and last name. When I looked at the value in Inspect element window, I noticed the value is displayed with an extra space. How I can fix this?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
In Magento 1.9.2 the middle name has been added to this column:
$ifnullFirst = $adapter->getIfNullSql('{{table}}.firstname', $adapter->quote(''));
$ifnullMiddle = $adapter->getIfNullSql('{{table}}.middlename', $adapter->quote(''));
$ifnullLast = $adapter->getIfNullSql('{{table}}.lastname', $adapter->quote(''));
$concatAddress = $adapter->getConcatSql(array(
$ifnullFirst,
$adapter->quote(' '),
$ifnullMiddle,
$adapter->quote(' '),
$ifnullLast
));
Unfortunately they did not really think about the case where a customer does not have a middle name. This is how the code should look like:
$ifnullFirst = $adapter->getIfNullSql('{{table}}.firstname', $adapter->quote(''));
$ifnullMiddle = $adapter->getIfNullSql('{{table}}.middlename', $adapter->quote(''));
$ifnullLast = $adapter->getIfNullSql('{{table}}.lastname', $adapter->quote(''));
$concatAddress = $adapter->getConcatSql(array(
$ifnullFirst,
$adapter->quote(' '),
$ifnullMiddle,
new Zend_Db_Expr('IF({{table}}.middlename IS NULL OR {{table}}.middlename="", "", " ")'),
$ifnullLast
));
You can copy the file to app/code/local/Mage/Sales/Model/Resource/Order.php
and patch it as described.
To fix the existing records, you can use this throwaway PHP script:
<?php
require 'app/Mage.php';
Mage::app();
Mage::getModel('sales/order')->getResource()->updateGridRecords(
Mage::getResourceModel('sales/order_collection')->getAllIds());
echo 'done';
Place it as fixordergrid.php
in the Magento root directory, execute and delete it. It might take some time, so you should better run it from the console, not in the browser:
php fixordergrid.php
Method 2
To further the accepted answer, it’s best practice not to edit the core magento code so using a rewrite with the fix would be better.
In config.xml
<global>
<models>
<sales_resource>
<rewrite>
<order>Yournamespace_Yourextension_Model_Sales_Order_Resource_Order</order>
</rewrite>
</sales_resource>
</models>
</global>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0