19

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?

1
  • 1
    Still present in 1.9.3.10
    – sv3n
    Sep 28, 2018 at 15:26

2 Answers 2

23

In Magento 1.9.2 the middle name has been added to this column:

Source: https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Sales/Model/Resource/Order.php#L93-L99

    $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
10
  • Ah, probably, the middle name was an empty string then and not null. I updated the code to reflect this as well 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?
    – Zinat
    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()); Aug 28, 2015 at 9:42
  • Please see update for instructions Aug 28, 2015 at 9:48
  • 1
    I didn't find it in the issue tracker, reported it now: magentocommerce.com/bug-tracking/issue/index/id/1202 Nov 24, 2015 at 21:28
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>
1
  • 3
    The 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. Nov 18, 2015 at 12:44

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.