diff --git a/Api/Order/RepositoryInterface.php b/Api/Order/RepositoryInterface.php index 3d344c93..741c4f1e 100644 --- a/Api/Order/RepositoryInterface.php +++ b/Api/Order/RepositoryInterface.php @@ -100,7 +100,7 @@ public function save(ChannableOrderData $entity): ChannableOrderData; * * @return int|bool */ - public function getByChannableId(int $channableId); + public function getByChannableId(int $channableId, ?int $storeId = null); /** * @param array $orderData diff --git a/Model/Order/Repository.php b/Model/Order/Repository.php index 5b742b09..bfbe11e6 100755 --- a/Model/Order/Repository.php +++ b/Model/Order/Repository.php @@ -165,7 +165,7 @@ public function delete(DataModel $entity): bool */ public function createByDataArray(array $orderData, int $storeId): DataInterface { - $channableOrderId = $this->getByChannableId((int)$orderData['channable_id']); + $channableOrderId = $this->getByChannableId((int)$orderData['channable_id'], $storeId); if ($channableOrderId) { $channableOrder = $this->get((int)$channableOrderId); $channableOrder->setAttempts($channableOrder->getAttempts() + 1); @@ -190,9 +190,9 @@ public function createByDataArray(array $orderData, int $storeId): DataInterface /** * {@inheritDoc} */ - public function getByChannableId(int $channableId) + public function getByChannableId(int $channableId, ?int $storeId = null) { - return $this->channableOrderResource->getByChannableId($channableId); + return $this->channableOrderResource->getByChannableId($channableId, $storeId); } /** diff --git a/Model/Order/ResourceModel/ResourceModel.php b/Model/Order/ResourceModel/ResourceModel.php index f5928afe..4b06fef0 100644 --- a/Model/Order/ResourceModel/ResourceModel.php +++ b/Model/Order/ResourceModel/ResourceModel.php @@ -61,13 +61,19 @@ public function __construct( * @param int $channableId * @return bool */ - public function getByChannableId($channableId) + public function getByChannableId($channableId, ?int $storeId = null) { $connection = $this->getConnection(); $select = $connection->select() ->from($this->getTable('channable_orders'), self::ID_FIELD_NAME) ->where('channable_id = :channable_id'); $bind = [':channable_id' => $channableId]; + + if ($storeId !== null) { + $select->where('store_id = :store_id'); + $bind[':store_id'] = $storeId; + } + return $connection->fetchOne($select, $bind); } diff --git a/Service/Order/Process/GetCustomIncrementId.php b/Service/Order/Process/GetCustomIncrementId.php index ce48b98a..4ee8d251 100644 --- a/Service/Order/Process/GetCustomIncrementId.php +++ b/Service/Order/Process/GetCustomIncrementId.php @@ -57,7 +57,7 @@ public function execute(array $orderData, StoreInterface $store): ?string $channelId = $orderData['channel_id']; $prefix = $this->configProvider->getOrderIdPrefix((int)$store->getId()); if ($this->configProvider->stripChannelId((int)$store->getId())) { - $newIncrementId = $prefix . preg_replace('/[^a-zA-Z0-9]+/', '', $channelId); + $newIncrementId = $prefix . preg_replace('/[^a-zA-Z0-9-]+/', '', $channelId); } else { $newIncrementId = $prefix . preg_replace('/\s+/', '', $channelId); } @@ -73,9 +73,12 @@ public function execute(array $orderData, StoreInterface $store): ?string ->getLastItem(); if ($lastOrder->getIncrementId()) { - $lastIncrement = explode('-', $lastOrder->getIncrementId()); - $newIncrementId = substr($lastOrder->getIncrementId(), 0, -(strlen(end($lastIncrement)) + 1)); - $newIncrementId .= '-' . (end($lastIncrement) + 1); + $suffix = substr($lastOrder->getIncrementId(), strlen($newIncrementId) + 1); + if (is_numeric($suffix)) { + $newIncrementId .= '-' . ((int)$suffix + 1); + } else { + $newIncrementId .= '-1'; + } } else { $newIncrementId .= '-1'; }