一些商家希望自定义订单号或发票号,以使其与Magento 2默认情况下的产品号不同。他们可能希望数字包含更多信息,或者他们可能具有不应该使用的现有格式’不能改变。也许这些数字需要从以前的网站中删除(或者他们只是不这样做)’看起来还不够漂亮)。这些数字每个都有一个增量ID,并且用于创建它们的值存储在数据库中,并且无法通过admin进行配置,因此我们’我需要一些SQL来进行定制。无需再费周折…
我们可以分别调整订单,发票,贷项凭单,& Shipments:
The 字首, 后缀, 起始值, 和 步 are stored 在 the database, while the 垫长 is set 在 the code. Before changing them, let’s see how they’全部用于生成增量ID:
In Magento 2, the method for determining the 在 crement ID is 在 Magento\SalesSequence\Model\Sequence
.
The 模式 is set as: a string, plus a 9-digit number (padded with 0s), plus another string.
The getCurrentValue()
method 返回s a new 在 crement ID according 至 the 模式 as: The 字首, plus the 返回 value of the 计算当前值()
method, plus the 后缀. The 计算当前值()
返回s the main number:
1 |
返回 ($这个->lastIncrementId - $这个->元->getActiveProfile()->getStartValue()) * $这个->元->getActiveProfile()->getStep() + $这个->元->getActiveProfile()->getStartValue(); |
为了演示,我们’ll work with the 在 crement ID for 订购s, so $ 这个->lastIncrementId
is the last value 在 the sequence_value
column of the sequence_order_1
table 在 the database. (If we were working with the 在 voice 在 crement ID, $ 这个->lastIncrementId
would come from the sequence_invoice_1
table.) The 1
在 the table 名称 is the store ID for the first store view. If you have another store view, you would use the sequence_order_n
table (where n
is your store ID).
So, 计算当前值()
subtracts 起始值
从 the last sequence_value
, multiplies by 步
, 和 then adds 起始值
. The result is added between the 字首 和 后缀.
我们可以将整个方法表示为一个数学公式:
在 crement_id
= 字首
+ ((sequence_value
– 起始值
) * 步
+ 起始值
) {padded 至 X
digits} + 后缀
sequence_value
starts at 1 和 always 在 creases by 1 when a new 订购 is created [or 在 voice, etc].
Initially, 起始值
, 和 步
are each 1, 和 字首
和 后缀
are undefined.
If we plug these values 在 to our formula, we can predict the 在 itial 订购 在 crement ID:
在 crement_id
= ''
+ ((1
– 1
) * 1
+ 1
) {padded 至 9 digits} + ''
增量ID =((1 –1)* 1 +1){填充至9位数字}
增量ID =1 {padded 至 9 digits}
增量ID =000000001
(This is consistent with the first 订购 在 crement ID shown 在 sales_order.increment_id
在 the database: 000000001)
The 字首 和 后缀 are simple. They prepend 和 append the 在 crement-ID number with the values stored 在 sales_sequence_profile .prefix
& sales_sequence_profile .suffix
, respectively. Changing the 字首 can be an easy way 至 lengthen the 在 crement ID or make it start with something besides 0. The 后缀 could be used 至 add 0s, 至 make the number appear 至 在 crease by 10 or 100 each time (as an example). Alternatively, we could use these values 至 stylize the 在 crement ID or make it proprietary, so 在 这个 case, let’只需添加一个前缀“CL-” 和 use “-M2” for our 后缀.
(When we create the next 订购, sequence_order_1.sequence_value
在 creases from 1 至 2, but 起始值
和 步
are still each 1.)
将其插入我们的公式中:
在 crement_id
= 字首
+ ((sequence_value
– 起始值
) * 步
+ 起始值
) {padded 至 X
digits} + 后缀
增量ID =‘CL-‘ + ((2 –1)* 1 +1){填充至9位数字} +‘-M2’
增量ID =‘CL-‘+ 2 {填充为9位数字)+‘-M2’
增量ID =‘CL-000000002-M2’
(When we create 这个 订购, the last row of sales_order.increment_id
should be consistent with our calculation.)
The 步 is stored 在 sales_sequence_profile .step
. It is 1 by default 和 should not be less than 1.
We can use it 至 在 crease our 订购 在 crement-ID number by a certain amount each time a new 订购 is created (or “step” it up). For example, because 步
is 1 by default, our last 在 crement-ID number “stepped” up by 1 from 000000001 至 000000002. However, when we change the 步, the 在 crement ID will “shuffle” 上e time before it follows the new 模式.
为了演示,让’s set the 步 至 100. (When we create the next 订购, sequence_order_1.sequence_value
在 creases from 2 至 3, but 起始值
is still 1.)
插入:
在 crement_id
= 字首
+ ((sequence_value
– 起始值
) * 步
+ 起始值
) {padded 至 X
digits} + 后缀
增量ID =‘CL-‘ + ((3 –1)* 100 +1){填充到9位数字} +‘-M2’
增量ID =‘CL-‘+ 201 {填充至9位数字)+‘-M2’
增量ID =‘CL-000000201-M2’
如您所见,增量ID号没有“step”比上一个值增加100(000000002)。 This is because it 洗牌s 上 the first change. However, the next 在 crement ID should follow the new 模式. (The next 订购 在 creases sequence_value
从 3 至 4.)
增量ID =‘CL-‘ + ((4 –1)* 100 +1){填充到9位数字} +‘-M2’
增量ID =‘CL-‘+ 301 {填充至9位数字)+‘-M2’
增量ID =‘CL-000000301-M2’
这次是增量编号“stepped”如我们希望的那样,比之前的值增加了100000000201 至 000000301)。
The 起始值 is stored 在 sales_sequence_profile .start_value
. It is 1 by default 和 cannot be less than 0. (Also, it should not be both greater than the last sequence_value
和 less than 步
, because that would make the 在 crement-ID number negative.)
The 起始值 is somewhat unintuitively 名称d, as it reduces the amount by which 步
is multiplied, 和 then is added back 至 the 在 crement-ID number. For example, because 起始值
is 1 by default, our last two 在 crement-ID numbers (000000201 和 000000301) effectively have a 1 added 至 a multiple of the 步
value (which is 100).
当我们更改起始值时,增量ID将“shift” 上e time before it follows the 模式 again. To demonstrate, we’ll use a 起始值 of 3. (When we create the next 订购, sequence_order_1.sequence_value
在 creases from 4 至 5, 和 步
is still 100.)
当我们插入它时:
在 crement_id
= 字首
+ ((sequence_value
– 起始值
) * 步
+ 起始值
) {padded 至 X
digits} + 后缀
增量ID =‘CL-‘ + ((5 –3)* 100 + 3){填充至9位数字} +‘-M2’
增量ID =‘CL-‘+ 203 {填充至9位数字)+‘-M2’
增量ID =‘CL-000000203-M2’
As you can see, the amount by which 步
was being multiplied got reduced, 和 the amount added 至 the end of the 在 crement-ID number 在 creased (instead of “stepping” from 000000301 至 000000401,“shifts” 至 000000203)。
下一个增量ID应该“step” up as usual. (The next 订购 在 creases sequence_value
从 5 至 6.)
增量ID =‘CL-‘ + ((6 –3)* 100 + 3){填充至9位数字} +‘-M2’
增量ID =‘CL-‘+ 303 {填充为9位数字} +‘-M2’
增量ID =‘CL-000000303-M2’
这次是增量编号“stepped”达到预期值,并且将起始值3添加到末尾。
(如果此更改似乎是设置增量ID的不寻常方法,请不要’t worry about it. The 在 tention was just 至 demonstrate how 起始值
affects the 模式.)
如果我们想设置一个“starting”使其看起来好像增量ID最初大于1并从那里开始计数的数字(例如为您的第一张银行支票编号1000),’t actually use the 起始值
property. (However, because it has that 名称, it seemed helpful 至 address the matter under 这个 section.)
To set a minimum number for our 在 crement IDs, we need 至 add a value 至 sequence_order_1.sequence_value
.
为了演示,让’s add a sequence_value
of 1006 和 put 步
和 起始值
back 至 1. (When we create the next 订购, sequence_order_1.sequence_value
在 creases from 1006 至 1007, 和 步
和 起始值
are now 1 again.)
根据公式:
在 crement_id
= 字首
+ ((sequence_value
– 起始值
) * 步
+ 起始值
) {padded 至 X
digits} + 后缀
增量ID =‘CL-‘ + ((1007 –1)* 1 +1){填充至9位数字} +‘-M2’
增量ID =‘CL-‘+ 1007 {填充为9位数字)+‘-M2’
增量ID =‘CL-000001007-M2’
现在它’s as if the first 订购 在 crement ID was 1000 和 we “stepped” up from there.
Note: The sequence_order
tables have an 自动递增
value, so the above method 上ly works if the 在 serted sequence_value
is higher than the previous. If you’ve tried 至 shorten the 订购 ID 这个 way unsuccessfully, you should check the 自动递增
value for the related sequence table:
1 |
节目 创建 表 sequence_order_1; |
If the 自动递增
value 在 the 返回ed query is higher than the sequence_value
number you’想换成你’需要运行如下查询:
1 |
改变 表 sequence_order_1 自动递增 = 1006; |
增量ID号的填充长度在代码库中确定,并且’不受任何数据库属性的影响。
In \Magento\SalesSequence\Model\Sequence
, the getCurrentValue()
method uses a formatted-string-print function 至 set the 模式 of the 在 crement-ID number:
1 2 3 4 5 6 |
返回 冲刺( $这个->模式, $这个->元->getActiveProfile()->getPrefix(), $这个->计算当前值(), $这个->元->getActiveProfile()->getSuffix() ); |
“$this->pattern” comes from the constant DEFAULT_PATTERN
, which is 在 itially: %s%'.09d%s
.
The 在 crement-ID number is determined by the middle section: %'.09d
. The '.0
sets “0” as the padding character 和 sets the number of digits 至 display as the value that follows, which 在 这个 case is 9
. The d
presents the number as a [signed] decimal. This means that by default, the 在 crement-ID number will be a signed decimal with 9 digits, padded with 0s. To demonstrate, we’ll set the pad-length 至 6. (When we create the next 订购, sequence_order_1.sequence_value
在 creases from 1007 至 1008, while 起始值
和 步
are still 1.)
使用我们的公式:
在 crement_id
= 字首
+ ((sequence_value
– 起始值
) * 步
+ 起始值
) {padded 至 X
digits} + 后缀
增量ID =‘CL-‘ + ((1008 –1)* 1 +1){填充至6位数字} +‘-M2’
增量ID =‘CL-‘+ 1008 {填充为6位数字)+‘-M2’
增量ID =‘CL-001008-M2’
如您所见,增量ID号现在只有6位数字,而不是9位数字。
In the database, the sales_sequence_profile
table sets the 模式 for the 在 crement ID 上 each entity 类型 (订购
, 在 voice
, creditmemo
, 和 shipment
) at each store view. We need 至 make our changes for store view 1, which is set 上 rows 5–8. (These rows set the 4 entity 类型s respectively.) 为了演示,我们’ll work with the 订购 在 crement ID, so we’ll be changing row 5 of sales_sequence_profile
(元_id = 5
)。
这里’s what sales_sequence_profile
looks like by default:
The following are the SQL queries (plus the line of code) 至 set each property 至 the value used 在 our examples. (Because we worked with the 在 crement ID for Orders, the 元_id
在 each query below is set 至 5, but you could also use 6, 7, or 8, 至 change the 在 crement IDs for Invoices, Creditmemos, 和 Shipments, respectively.)
1 |
更新 `sales_sequence_profile ` 组 `字首` = 'CL-' 哪里 `元_id` = 5; |
1 |
更新 `sales_sequence_profile ` 组 `后缀` = '-M2' 哪里 `元_id` = 5; |
1 |
更新 `sales_sequence_profile ` 组 `步` = 100 哪里 `元_id` = 5; |
1 |
更新 `sales_sequence_profile ` 组 `起始值` = 3 哪里 `元_id` = 5; |
1 |
插 进入 `sequence_order_1` (`sequence_value`) 价值 ('1000'); |
The constant DEFAULT_PATTERN
is set 在 : /vendor/magento/module-sales-sequence/Model/Sequence.php,在第19行。
We can change 这个 在 a custom module by creating etc/di.xml
with the following contents:
1 2 3 4 5 6 7 8 |
<?XML文件 版="1.0"?> <配置 XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" si:noNamespaceSchemaLocation=“ ur:magento:框架:ObjectManager / etc / 配置.xsd”> <类型 名称=“ Magento \框架\ DB \ Sequence \ SequenceInterface”> <论点> <论据 名称="模式" si:类型=“串”>%s%'.06d%s</论据> </论点> </类型> </配置> |
You can now have full control over your 在 crement IDs. Although they sometimes seem 至 change unexpectedly when you adjust certain values, the formula should help you predict the 模式:
在 crement_id
= 字首
+ ((sequence_value
– 起始值
) * 步
+ 起始值
) {padded 至 X
digits} + 后缀
快乐增加!
37 评论s
这是修改增量ID的方式的非常有用且完整的说明。谢谢你!
我只有一句话。可以在该文件夹的magento 2中找到需要修改的文件(Sequence.php):.. /vendor/magento/module-sales-sequence/Model/Sequence.php
@peinture–感谢您的评论。您可能会注意到,您提到的文件路径已经在帖子结尾附近列出了,“Make it Happen” section (“Pad-Length”小节)。如果我有帮助的话’d在主目录中列出文件路径“Pad-Length” section?
谢谢!非常详细和有用!
你好,
can the 后缀 be the year of the creation of the 订购?
谢谢
后缀是来自数据库的固定字符串,该字符串’s作为传递给sprintf()的第4个参数添加到Magento \ SalesSequence \ Model \ Sequence :: getCurrentValue()中的增量ID中。如果创建首选项来重新定义getCurrentValue(),则可以将日期设置为变量,然后将其传递给sprintf()而不是后缀。
关于填充:干净的解决方案似乎是一个di.xml注入,如下所述: //github.com/magento/magento2/issues/5541#issuecomment-249501943。我不’尚不知道该怎么做,但对我来说听起来似乎很有意义。如果您确实更新了文章,很高兴。
确认例如
%s%’.8d%s
然后在app / etc / di.xml中将数字填充到8位数字。
哎呀上面的注释中有xml标记,但是注释功能在早餐时全部吃了。因此,请在我的第一条评论的链接中查看conf-snippet的示例
@费利克斯–谢谢你的建议。看起来XML解决方案有效,所以我相应地更新了本文。
This is a really dumb question but my Magento 2.2.3 has 订购 numbers like 2000023232
我如何摆脱领先的20000?
@克里斯– I can’t肯定地说而不看数据库中增量ID的配置方式,但是随着您输入的数字的长度,似乎’s a 字首 of “2”. (See the the “Prefix/Suffix” section对于an explanation of how it works.) In the “Make It Happen” section, 在下面“Prefix” sub-header, there’s a sample SQL query for changing the 字首. To remove it, just set it 至 null 在 the database. For example:
更新 `sales_sequence_profile ` 组 `prefix` = NULL 哪里 `meta_id` = 5;
The元_id
may vary, so reference thesales_sequence_meta
table 和 get the元_id
从 whichever row hassequence_order_N
, whereN
is the store view you’re 配置uring (or0
对于the default scope).为了摆脱那些零,你’我需要更改焊盘长度。 (请参阅 “Pad-length” section对于an explanation of how it works.) If you don’如果不希望有任何零,您可以使用代码库中的自定义模块完全删除填充字符。在里面 “Make It Happen” section, 在下面“Pad-length” sub-header, there’s a sample
di.xml
file for changing the padding. To remove the padding, we should be able 至 pass the following value 在 the “pattern” 论据:%s%d%s
(on line 5 of the sample file). This should set the main part of the number as simply a [signed] decimal, without any padding.我希望这有帮助!
@Chris:我想您必须在项目中使用2家商店,对于1家商店,它将作为“10000”对于第二家商店,它将作为“20000”.
ðŸ™,
这是一篇很棒的文章;您的读者真的可以从您的帖子中得到点点滴滴,来建立自己需要的商店。
I figured I would post my setup, both 在 订购 至 get a confirmation that our set up makes sense, 和 also 在 case anyone is looking 至 do something similar.
我们有2个商店视图:英语(1)和法语(2)。
We wanted every 订购/invoice/shipment/credit 至 在 crement sequentially regardless of store view. In 订购 至 accomplish 这个, 在 ‘sales_sequence_meta’,我们将每条记录设置为‘sequence_XXX_0’,默认值为0。
我们为中的所有商店视图设置相同的前缀和后缀‘sales_sequence_profile ’,一切似乎都按预期进行。
@塞缪尔–感谢您的反馈!一世’我很高兴这篇文章很有帮助。
根据您所描述的`sales_sequence_meta`, it sounds like you’实质上是将每个增量ID设置为将各自的序列表用于默认商店视图。 (例如。`entity_type` of ‘order’ for `store_id`1和2都使用`sequence_order_0` table.) I think 这个 does make sense 在 订购 至 make everything 在 crement sequentially across all store views.
谢谢布伦丹,
另一个问题:我注意到有一个领域‘increment_last_id’ 在 table ‘eav_entity_store’.
在为您的文章设置增量ID时更新其他表时,是否需要手动修改此设置?
I’我不能完全确定该表的用途,但是在将值与序列表进行比较并搜索了使用代码后,它似乎与数据迁移有关。似乎注意到“Last Incremented Id”来自相应的Magento 1网站,并将其分别设置为第一个`sequence_value`一个新的Magento 2网站。我不’t think it’在这种情况下直接相关,因为我相信顺序表(例如` sequence_order_0`)在生成新的增量ID时具有权威性。
那 makes sense, as we did 在 deed do an M1->M2 migration.
最后一个问题:简单地在表格中手动设置焊盘长度是否不起作用‘eav_entity_type’, field ‘increment_pad_length’ ?
不,我’m pretty sure that’也用于数据迁移。正如在 “Pad-length” section, the 在 crement ID is actually calculated 在
\Magento\SalesSequence\Model\Sequence::getCurrentValue()
with a 格式化字符串功能 that uses a 模式 set 在 the constantDEFAULT_PATTERN
. This is where the padding is specified. It can be changed as outlined 在 the “Make it happen” section, 在下面“Pad-length” sub-heading.嗨布伦丹,
我将再次分享有关设置顺序增量而不考虑商店ID的更多见解。
In ‘sales_sequence_meta’,我将每种实体类型的sequence_table设置为默认存储区一(即“_0”)。为了明确起见,我们具有默认的商店视图(ID = 0)和2种语言(ID 1和2)。每个类型的实体“order” (for store ID 1, 2 &3)有顺序表‘sequence_order_0’ associated 至 it.
This has been working well 和 the 订购 numbers are 在 crementing sequentially across all store views.
但是,当执行
bin / magento设置:升级
我们得到了错误“发现独特的违规行为”.
原来有检查以确保该字段中没有重复项‘sequence_table’.
解决方法是返回默认设置以运行setup:upgrade。
What do you think would be the best workaround 在 订购 至 not have 至 manually edit the ‘sales_sequence_meta’每次我们需要进行设置时的表:升级吗?
@塞缪尔– I’m honestly not sure at 这个 point. Perhaps you could create an UpgradeSchema script 至 remove the unique constraint from the table, but it may be there for some other reason. Maybe 在 stead it would be best 至 create a custom module 至 update the sequence tables of a certain 类型 across all store views, so for example, if you place an 订购 在 store view 1,
sequence_order_0
和sequence_order_2
would get updated at the same time assequence_order_1
, 和 I would think that would keep the 在 crement IDs 在 sync, without having 至 violate the unique constraint 在sales_sequence_meta
. (just a couple ideas)你好布伦丹,
感谢您的精彩帖子。
我正面临一个问题,如果您能帮助我,那真的很好。
这里 is my issue:
我已经覆盖了“pattern” 在 side di.xml (%s%’.05d%s), but 订购 number is generating 上e extra number(1) e.g: Your 订购 number is: 165898.
它应该是“65898”. Why the extra “1” is generating before the 订购 number.
所以首先我认为这是一个前缀,然后检查数据库>>sales_sequence_profile 表,如果找到,则前缀和后缀字段都显示为NULL。
你能帮忙吗?
我认为默认情况下,仅默认配置范围的前缀为NULL,而store-view 1的前缀为“1”,而商店视图2的前缀为“2”等等。您可能需要检查以确保sales_sequence_profile 中的所有前缀字段均为NULL。如果是的话,我不会’不知道还会导致什么。
您好Brendan,感谢您的精彩文章。我可以用年,月,日,小时,分钟和秒的组合创建一个ID。 (对不起我的英语不好– Google Translator)
How would I go about getting my 订购 number 在 这个 format: YYYY-MM-XXXXX
(Y = Year, M = Month, XXXXX = 5 digit 订购 number that resets every month)
@第一大主教塞萨尔·帕拉西奥(Cesar Palacio)–要修改增量ID的格式,您可以’ll need 至 plug 在 至
Magento\SalesSequence\Model\Sequence
(as described 在 the “Formula” section above), probably 开始 with thegetNextValue()
method, which has access 至 thesequence_value
在 the database.Brendan出色的关节(BTW Magento支持团队使用增量ID来解释某些情况)。让我分享我们期望的一个问题(从客户的角度来看)。
We are running Magent Commerce Cloud 和 the architecture cloud has create for each 订购 created a sequence of 3 jumping 至 3. XXXXX3 至 XXXXX6.
这里 is the official explication //mariadb.org/auto-increments-in-galera/
For the project 这个 is cousing huge problem bc that number is used 在 ternally for the rest of the extra systems (ERP 和 warehouse) we are lousing 2 numbers for each 订购.
支持团队告诉我们,我们需要创建一个自定义代码来处理这种情况。
这是一个问题,根据您的经验,应该可以使用此配置恢复或正确跳转到1到1。
最好,
亚历杭德罗
看到 ‘Step’ section (和 ‘Step’ subsection 在 ‘Make it Happen’ 部分)。
(
更新
sales_sequence_profile组
步= 3;
)嗨布伦丹,
谢谢你提供的详情。
我安装了一个免费的扩展程序,该扩展程序使所有四个实体(订单,发票,贷方,发货)的数字保持相同。
我使用的扩展名是:Magento 2的Fooman同订单发票号。
但是编号仍然是Magento 2的默认设置。
如果我按照您的指示将数字设置为Ord-####,INV-####,CRD-###,SHP-####,扩展名是否仍将数字部分保持不变?还是我可以在数据库中启用一些使数字保持不变的功能?
亲切的问候,罗伊
I’m afraid I’我不熟悉此扩展程序。
那’s ok, I understand …但是,如果我设置了Ord-####,INV-####,CRD-##之类的格式,您知道如何确保所有四个实体(订单,发票,贷方,货件)的编号保持相同#,SHP-####?
好帖子
What happens when the 订购s are 在 creased more than the padded digits i.e. if I set 字首 as 702, padded digits as 4 和 no 后缀 so our system can take upto 9999 订购s 和 what happens if 10000 订购 is placed. Could you please clear 这个 as well.
填充不会从基值中删除数字。仅在需要时添加它们。
因此,与填充数相同(或更多)的字符数的任何值都不会受到影响。
–我通过di.xml填充了@ 7
–通过更改sequence_order_1中的AUTO_INCREMENT值来更改我的初始订单ID
–我想要一个7位数的值,但不小心输入8(10002500),这没问题。
–当尝试再次通过sequence_order_1中的AUTO_INCREMENT值将该值(10002500)更改为1002500(7位)时,什么也没有发生。当我需要将其恢复为7时,它将始终恢复为8位数。继续了解其工作原理。
I’我不熟悉这种特殊情况。
If you already changed
di.xml
back 至 7 (undoing accidental 8), then perhaps a cache flush 和/or clearing of generated code is needed.My current 订购 id like 100000010 how 至 start with “2” eg: 200000010
这很可能是前缀。看到 字首/Suffix部分 (and example 在 实现它部分)。
(Possible example:
更新 `sales_sequence_profile ` 组 `prefix` = '2' 哪里 `meta_id` = 5;
)