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:
<global> <events> <customer_save_before> <observers> <customer_data_will_save> <type>singleton</type> <class>custnum/observer</class> <method>customerSaveBefore</method> </customer_data_will_save> </observers> </customer_save_before> <events> </global>
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') ->getEntityType('customer') ->fetchNewIncrementId($customer->getStoreId()); $customer->setIncrementId($incrementId); } }
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 1.6.2.0. In the block classes, all you need to change is:
$this->getUrl('sales/order/track', array('order_id' => $order->getId()));
to
$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');
to
$orderId = $this->getRequest()->getParam('order_id');
and
$order = Mage::getModel('sales/order')->load($orderId);
to
$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: