Changing increment_model for orders
-
20-03-2021 - |
Pergunta
i want to change default increment_model for orders only. I create new module and add new Class:
Class Order extends \Magento\Eav\Model\Entity\Increment\AbstractIncrement {
...
}
i updated table eav_entity_type ,the row with order in column instead of
Magento\Eav\Model\Entity\Increment\NumericValue
i've put
Vendor\OrderNumber\Model\Entity\Increment\Order
but this seems not working at all.
I put breakpoints in all increments classes and not single one are hit.
I find only this post -> https://magento.stackexchange.com/a/135109
But in comments i read that this will not work in magento >=2.2 ( i'm using 2.3 Commerce) .
Can someone help me find correct solution to this ?
Edit - solution
My solution to this problem is to create new module:
/app/code/Vendor/OrderNumber
and add files:
/app/code/Vendor/OrderNumber/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Quote\Model\ResourceModel\Quote" type="Vendor\OrderNumber\Model\ResourceModel\Quote\Quote" />
</config>
/app/code/Vendor/OrderNumber/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_OrderNumber" setup_version="0.0.1">
</module>
</config>
/app/code/Vendor/OrderNumber/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_OrderNumber',
__DIR__
);
/app/code/Vendor/OrderNumber/Helper/Order.php
<?php
namespace Vendor\OrderNumber\Helper;
use Magento\Framework\App\Helper\{AbstractHelper, Context};
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
/**
* Class Order
* @package Vendor\OrderNumber\Helper
*/
class Order extends AbstractHelper
{
/**
* @var OrderCollectionFactory
*/
private $orderCollectionFactory;
/**
* Order constructor.
* @param OrderCollectionFactory $orderCollectionFactory
* @param Context $context
*/
public function __construct(
OrderCollectionFactory $orderCollectionFactory,
Context $context
)
{
parent::__construct($context);
$this->orderCollectionFactory = $orderCollectionFactory;
}
/**
* @param $incrementId
* @return string
*/
public function getHashedIncrementId($incrementId): string
{
$newIncrementId = $this->hash($incrementId);
if ($this->isOrderIncrementIdExists($newIncrementId)) {
$newIncrementId = $this->getHashedIncrementId($incrementId);
}
return $newIncrementId;
}
/**
* @param $incrementId
* @return bool
*/
protected function isOrderIncrementIdExists($incrementId): bool
{
$orderCollection = $this->orderCollectionFactory->create();
$orderCollection->addFieldToFilter('increment_id', $incrementId)
->getSelect()->reset('columns');
$orderCollection->getSelect()->columns('entity_id');
return $orderCollection->getFirstItem()->getId() ? true : false;
}
/**
* @param mixed $oldIncrementId
* @return int
*/
private function hash($oldIncrementId)
{
$newIncrementId = 0;
$oldIncrementId = (int)$oldIncrementId;
// doing stuff with $oldIncrementId;
return $newIncrementId;
}
}
/app/code/Vendor/OrderNumber/Model/ResourceModel/Quote/Quote.php
<?php
namespace Strix\OrderNumber\Model\ResourceModel\Quote;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
use Magento\SalesSequence\Model\Manager;
use Vendor\OrderNumber\Helper\Order as OrderHelper;
/**
* Class Quote
* @package Vendor\OrderNumber\Model\ResourceModel\Quote
*/
class Quote extends \Magento\Quote\Model\ResourceModel\Quote
{
/**
* @var OrderHelper
*/
private $orderHelper;
/**
* Quote constructor.
* @param Context $context
* @param Snapshot $entitySnapshot
* @param RelationComposite $entityRelationComposite
* @param Manager $sequenceManager
* @param OrderHelper $orderHelper
* @param string $connectionName
*/
public function __construct(
Context $context,
Snapshot $entitySnapshot,
RelationComposite $entityRelationComposite,
Manager $sequenceManager,
OrderHelper $orderHelper,
$connectionName = null
)
{
parent::__construct($context, $entitySnapshot, $entityRelationComposite, $sequenceManager, $connectionName);
$this->orderHelper = $orderHelper;
}
/**
* @param \Magento\Quote\Model\Quote $quote
* @return string
*/
public function getReservedOrderId($quote)
{
return $this->orderHelper->getHashedIncrementId(parent::getReservedOrderId($quote));
}
}
This solution worked for me and i can modify order number in the way i want.
Nenhuma solução correta