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?
2 Answers
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
-
Ah, probably, the middle name was an empty string then and not null. I updated the code to reflect this as well Commented Aug 28, 2015 at 9:32
-
Thanks for the kind reply. It works for the new registered customers. How about the old records, can it be fixed through db query?– ZinatCommented Aug 28, 2015 at 9:39
-
I would try to force recalculation of the table once with
Mage::getModel('sales/order')->getResource()->updateGridRecords(Mage::getResourceModel('sales/order_collection')->getAllIds());
Commented Aug 28, 2015 at 9:42 -
-
1I didn't find it in the issue tracker, reported it now: magentocommerce.com/bug-tracking/issue/index/id/1202 Commented Nov 24, 2015 at 21:28
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>
-
3The accepted answer does not edit the core Magento code, @fschmengler is recommending a local code pool override. This particular model is rewritten often by modules, and therefore the code pool override is probably better. Commented Nov 18, 2015 at 12:44