Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Custom Order and Customer Numbers in Magento: Part 1
Alexander Turiak Avatar

In Magento, order numbers are generated starting from 100000001 and up by default. Many companies want to use a custom value so that customers don’t know exactly how many orders have been placed before. Others need a custom value to match other parts of their system. The field increment_id is present in various entity tables to store this number.

Different settings, regarding the start sequence and number format, are defined in the eav_entity_type and eav_entity_store tables. Let’s have a closer look at what is possible to control:
eav_entity_store table has two options of interest: increment_prefix and increment_last_id. increment_prefix specifies which prefix will be added to number and by default equals «1». increment_last_id defines the last entity number. Here we can change the starting number of orders, invoices and shipments.
eav_entity_type table has settings which allow us to change the number format. These are increment_pad_length which is used to set the length of the number excluding its prefix and increment_pad_char used to specify the character used to pad the number to required length. By default it is zero. Another important field is increment_model which specifies the class that is used to generate numbers. This is Mage_Eav_Model_Entity_Increment_Numeric for all default entities.

 

For instance, if we want to change our order number and add a custom prefix and current year so it looks like OR12000001. Here is how to do it.
Note: Before first order is placed, there are no records in the eav_entity_store table, so you need to place an order or manually create a new record with entity_type_id = 5 and store_id = 1.
First, we need to update the increment_prefix field in eav_entity_store table to OR and also update increment_last_id field and set it to OR12002673. Next, in eav_entity_type, set the value of the increment_pad_length field to 6. This is length of the number without the OR12 prefix. Then, create class Mage_Eav_Model_Entity_Increment_Order in folder app/code/local/Mage/Eav/Model/Entity/Increment with the following content:

<?php 

class Mage_Eav_Model_Entity_Increment_Order extends Mage_Eav_Model_Entity_Increment_Abstract
{
    public function getNextId()
    {
        $last = $this->getLastId();

        if (strpos($last, $this->getPrefix()) === 0) {
            $last = (int)substr($last, strlen($this->getPrefix()) + 2);
        } else {
            $last = (int)$last;
        }

        $next = $last+1;

        return $this->format($next);
    }

    public function format($id)
    {
        $result  = $this->getPrefix() . date('y');
        $result .= str_pad((string)$id, $this->getPadLength(), $this->getPadChar(), STR_PAD_LEFT);
        return $result;
    }

}

Lastly, update the increment_model field for the order entity to eav/entity_increment_order to use the custom model for generating order numbers. Now you should be able to place orders with desired number format.

 

That’s it for the first part of the article. In the second part, we will discuss how to generate custom numbers for customer entities and how to hide the default Magento created order number (id) from your store frontend.
I hope you found this article helpful. Stay tuned for updates in our blog and feel to contact us for special requests.

Continue to the second part.

Read more: