Custom order and customer numbers in Magento: Part 2

Back to Part 1.

In Magento, customers don’t use special numbering by default; however it is possible to change this. We need to add a record to the eav_entity_store table with entity_type_id = 1, increment_prefix = CU and increment_last_id = CU001000.

To make it work, we use a module with an observer to generate the desired customer number. Let’s create a module called Atwix_Custnum and in its config.xml we will define the observer like this:


Next, create a class Atwix_Custnum_Model_Observer with method customerSaveBefore:

public function customerSaveBefore($observer)
    $customer = $observer->getCustomer();
    if (!$customer->getIncrementId()) {
        $incrementId = Mage::getSingleton('eav/config')

In addition, if you’d like to see this newly generated id in the admin grid, instead of the default id, you need to override Mage_Adminhtml_Block_Customer_Grid class by placing a copy of it in the local pool, for example, and change id column or add a new one with index = increment_id and type = text.

Next thing to do is to replace the default order id with the new “real order id” on the customer-side front-end. For this, we need to rewrite a couple of core classes in the local pool. While it is not the best approach, it allows us to quickly adhere the logic to our needs. You can read more about this technique in this Magebase article.
These classes are Mage_Sales_Controller_Abstract, Mage_Sales_Block_Order_Info_Buttons, Mage_Sales_Block_Order_History and Mage_Sales_Block_Order_Recent as of Magento CE In the block classes, all you need to change is:

$this->getUrl('sales/order/track', array('order_id' => $order->getId()));


$this->getUrl('sales/order/track', array('order_id' => $order->getRealOrderId()));

everywhere in the files.
In Mage_Sales_Controller_Abstract::_loadValidOrder() method change:

$orderId = (int) $this->getRequest()->getParam('order_id');


$orderId = $this->getRequest()->getParam('order_id');


$order = Mage::getModel('sales/order')->load($orderId);


$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

That’s it. Customers should now be able to see the new, custom order id instead of the default generic one.


I hope this article was helpful. Thank you for reading it!

Read more: