Having multiple store views share the same order increment_id number range

I'd imagine that this would be quite difficult. Increment id's are stored in the eav_entity_store table and unsurprisingly each store has it's own entry which is updated when an order (and quote, invoice etc) is created. To get all stores to use the same incrementer you would need to somehow rewrite this logic so it used the same row in the DB. Quite what impact this may have on other areas of the site is something else that would need to be considered.


You can override orders, shippings, invoices and credit memos increment model by rewriting the "eav/entity_increment_numeric" class providing custom logic in a Model of yours.

Take a look at the ancestor classes (Mage_Eav_Model_Entity_Increment_Numeric and Mage_Eav_Model_Entity_Increment_Abstract) in order to understand how to provide your own logic.

You can the differentiate logic between different entities by checking the $entityTypeCode parameter of the getNextId() function you will override.

Another (more invasive) way is that of specifying a different increment model for each entity type by overwriting (via install script) the value of the "increment_model" column of the "eav_entity_type" table. Personally I prefer the "rewrite" solution mentioned above.

Pay attention: increment ids have a uniqueness constraint in latest Magento versions so you can't store the same increment id for two different entities of the same type. In other words you can't have two different invoices with the same increment id.

Hope it helps.


While digging deeper, I realized that eav_entity_type.increment_per_store may be helpful.

It is. But only for the case, when you want all store views (globally, no matter which website they're defined in) of your Magento installation to share the same order increment_id number range.

This doesn't solve my specific issue, but maybe it's helpful to some others, so here we go:

To activate global sharing of your order numbers, set eav_entity_type.increment_per_store of the order entity to 0 ,

This leads to Mage_Eav_Model_Entity_Type::fetchNewIncrementId() using store_id = 0 when loading the eav_entity_store record of the order entity, no matter which store view it really belongs to.

If no such record exists, Magento creates one, using store_id and increment_prefix of 0.

public function fetchNewIncrementId($storeId = null)
{
    if (!$this->getIncrementModel()) {
        return false;
    }

    if (!$this->getIncrementPerStore() || ($storeId === null)) {
        /**
         * store_id null we can have for entity from removed store
         */
        $storeId = 0;
    }

    // Start transaction to run SELECT ... FOR UPDATE
    $this->_getResource()->beginTransaction();

    $entityStoreConfig = Mage::getModel('eav/entity_store')
        ->loadByEntityStore($this->getId(), $storeId);

    if (!$entityStoreConfig->getId()) {
        $entityStoreConfig
            ->setEntityTypeId($this->getId())
            ->setStoreId($storeId)
            ->setIncrementPrefix($storeId)
            ->save();
    }

    $incrementInstance = Mage::getModel($this->getIncrementModel())
        ->setPrefix($entityStoreConfig->getIncrementPrefix())
        ->setPadLength($this->getIncrementPadLength())
        ->setPadChar($this->getIncrementPadChar())
        ->setLastId($entityStoreConfig->getIncrementLastId())
        ->setEntityTypeId($entityStoreConfig->getEntityTypeId())
        ->setStoreId($entityStoreConfig->getStoreId());

    /**
     * do read lock on eav/entity_store to solve potential timing issues
     * (most probably already done by beginTransaction of entity save)
     */
    $incrementId = $incrementInstance->getNextId();
    $entityStoreConfig->setIncrementLastId($incrementId);
    $entityStoreConfig->save();

    // Commit increment_last_id changes
    $this->_getResource()->commit();

    return $incrementId;
}

This should work for any entity type using the eav/entity_increment_numeric model, like order, invoice, shipment and creditmemo.

Be aware though, that I couldn't find any official documentation of increment_per_store yet. And that there's no option in the Magento backend letting you configure this.

This may or may not mean, that it's not thought to be used officially.

Use at your own risk. If your changes wreak havoc, don't blame me. You've been warned^^