0

In Magento 2.2.6 they "fixed" an error, all invoices had the sequence number of the default store. This worked out for us because we need the invoices for all storeviews in a single sequence for Tax purposes.

The change was made in the file magento\module-sales\Model\ResourceModel\EntityAbstract.php

Now I want to revert this change without altering the Core Code. The code I want to use is: (From Magento 2.2.5)

    protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
    /** @var \Magento\Sales\Model\AbstractModel $object */
    if ($object instanceof EntityInterface && $object->getIncrementId() == null) {
        $object->setIncrementId(
            $this->sequenceManager->getSequence(
                $object->getEntityType(),
                $object->getStore()->getGroup()->getDefaultStoreId()
            )->getNextValue()
        );
    }
    parent::_beforeSave($object);
    return $this;
}

Now I tried Various options using my own Module with preference setting is the di.xml but I can get it to work. Magento only uses the original code and not the one I want it to use.

For Now I changed the Magento 2 Core Code but this is not the right way.

Any thoughts on this? I'm really stuck.

2
  • Did you try the suggested solution for overwriting the assignemnet of the IncrementId? Any feedback on that?
    – HelgeB
    Commented Feb 12, 2019 at 11:11
  • Hi thanks I'm already looking for the right function to edit. There are multiple setIncrementId functions :)
    – Hans Bos
    Commented Feb 12, 2019 at 17:58

1 Answer 1

1

If you need that only for the invoices you can try to create a plugin before the public method save() or even the public method beforeSave() of the invoice model (otherwise you need that for every single sales model). In your method you can set the increment Id following your requirements.

The _beforeSave() method in the Abstract core model checks if there is already an increment id assigned and skip the generation.

I don't think it's possible to overwrite the protected _beforeSave() method.

Hope that helps.

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.