Magento2 - Shared increment-id for multi-websites setup

Magento2 - Shared increment-id for multi-websites setup

June 14, 2020

Introduction #

By default Magento2 have different incrementIds for EAV entities for each store - for example for orders’ incrementId:

New order Storeview A Storeview B
Placed in A #1000000
Placed in A #1000001
Placed in B #1000000
Placed in A #1000002
Placed in B #1000001

However, in some cases you might to have this scenario represented this way:

New order Storeview A Storeview B
Placed in A #1000000
Placed in A #1000001
Placed in B #1000002
Placed in A #1000003
Placed in B #1000004

In this case - increment Id remains global and is incremented by 1 for every order placed, also in different stores.

You can get extension here: https://github.com/adamkarnowka/magento2-shared-increment-id

How it works #

For each of the entity type, you can find some settings in table eav_entity_type - where increment models, incrementation step any many other settings are defined. What is interesting for us - is increment_per_store column, which - in theory should do the trick. Our most interesting entities like order, invoice or creditmemo will have it set as 1 - which basically means that each store should have separate increment counter.

ALT

So - in order to have it working globally it should be enough to change it to 0 and it should be fine? Unfortunately not - if performs search for increment_per_store in Magento2 codebase, you will find it only in setup scripts in part of code that is never reached. It is probably leftover from Magento1, where this setting existed and was actually working.

Module to change logic #

In order to fix, I have created module which modify logic of fetching new incrementId. First, there is global confiigation setting, where you can define for which entity types you want to have global incrementation:

Configuration

Then, we create plugin on class Magento\SalesSequence\Model\Manager method beforeGetSequence where we check if processed entity is found in configuration - if no, then we return original method with original arguments (no change). However, when processed order is marked to be global, we invoke defauly method with $storeId param changed to 0. This way, for this entity - there will be only one increment model for this entity - and it will not depend on storeview scope.

Resources #

🍺 If you liked this article you might consider buying me a beer? ;)