Change increment_id length in sales_order table to Magento 2

I want to reduce the duration of the increment of the order of # 00000000001 to # 00001. I found some lessons for magento 1, but not for Magento 2. Help if someone knows how to complete this task.

thank

+4
source share
3 answers

In the directory of etcyour module, add a file di.xmlwith this content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\SalesSequence\Model\Sequence">
        <arguments>
            <argument name="pattern" xsi:type="string"><![CDATA[%s%'.05d%s]]></argument>
        </arguments>
    </type>
</config>

Thus, Magento will transmit a 5-digit pattern instead of the default pattern made in 9 digits.

, %s prefix suffix, sales_sequence_profile. meta_id ( , sales_sequence_meta). %'.05d , , , , Magento.

, :

  • : PX
  • : SX

, PX00001SX

+5

:

I already had an observer to save the client as a new client automatically after placing the order and before displaying the success page:

In event.xml (an existing observer that I made earlier)

<event name="sales_order_place_after">
    <observer name="customcheckout_customer" instance="Dufry\CustomCheckout\Model\Observer\SaveCustomer"/>
</event>

On the SaveCustomer.php observer (already existing observer that I did earlier):

    $order = $observer->getOrder();

    $increment = $order->getIncrementId();
    if(strlen($increment) > 9){
        $newIncrement = substr($increment, -8);
        $newIncrement = substr($increment,0,1).$newIncrement;
        $order->setIncrementId($newIncrement);
    }

    ...

    $order->save()

And it worked like a charm.

I did the second part of "substr" to save the prefix that was previously configured.

0
source

Source: https://habr.com/ru/post/1649139/


All Articles