1

By default, Commerce uses the {{number[:7]}} format to set an order's reference on order completion. Now (after the site already has orders and I can no longer reset the site's data), I need to use a different format for existing orders, because we're going to use it as the invoice number, which has to be sequential.

Is there a way to reset the order's references in such a way that I can set the format to {{object.dateCompleted|date('y')}}-{{ seq(object.dateCompleted|date('y'), 6) }}, run a script (for instance) and give all orders a new reference (2021-000001, 2021-000002, ...)?

1 Answer 1

2

As soon as you change the mini template for your order numbers, Commerce will start using that new thing...if you want the numbers part to reset, just use a new sequence name in the template.

You can start it at a particular number, too, if you wish, e.g. to start at 2022-000005:

{{ object.dateCompleted|date('y') ~ '-' ~ (seq("some_new_name",6) + 5) }}

If you want to remap existing orders to this format, you will probably be best doing that via some migration/module code...

Basically...loop over the orders in question and:

$order->reference = $whatever;
Craft::$app->getElements()->saveElement($order);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.