Add Dynamic Order Number Prefix [mm-yy-ordernumber]
-
02-10-2020 - |
Question
My client wants the current month and year as a prefix in order numbers. I have read many blogs but they are all for static prefix for order number.
I am looking for a solution in Magento 2.
Solution
You can do that with a custom increment model.
You can read about increment models here: How to set order increment id iterator in magento 1?
In Magento 2, the implementation is still the same. In your case, you would extend Magento\Eav\Model\Entity\Increment\NumericValue
and add a getPrefix()
method to implement your custom prefix:
namespace Stack\OrderNumber\Model\Entity\Increment;
use Magento\Eav\Model\Entity\Increment\NumericValue;
class DatePrefix extends NumericValue
{
public function getPrefix()
{
return \date('m-y');
}
}
Then update increment_model
for orders in the eav_entity_type
table and change it from Magento\Eav\Model\Entity\Increment\NumericValue
to your model (e.g. Stack\OrderNumber\Model\Entity\Increment\DatePrefix
)