getRequest()->getParam('customer')); if ($query === '') { $this->messageManager->addErrorMessage(__('Enter a customer ID or email.')); return $this->redirectToGrid(); } try { $collection = $this->filter->getCollection($this->collectionFactory->create()); $connection = $this->resource->getConnection(); $ordersTable = $this->resource->getTableName('esun_legacy_order'); $customersTable = $this->resource->getTableName('customer_entity'); $linked = 0; $skipped = 0; foreach ($collection as $order) { $select = $connection->select() ->from($customersTable, ['entity_id', 'email']) ->where('website_id = ?', (int)$order->getData('website_id')); if (ctype_digit($query)) { $select->where('entity_id = ?', (int)$query); } else { $select->where('LOWER(TRIM(email)) = ?', strtolower($query)); } $customers = $connection->fetchAll($select); if (count($customers) !== 1) { $skipped++; continue; } $connection->update( $ordersTable, ['customer_id' => (int)$customers[0]['entity_id'], 'link_status' => 'linked'], ['legacy_order_id = ?' => (int)$order->getId()] ); $linked++; } if ($linked > 0) { $this->messageManager->addSuccessMessage(__('%1 legacy order(s) were linked.', $linked)); } if ($skipped > 0) { $this->messageManager->addWarningMessage( __('%1 order(s) were skipped because the customer did not uniquely match the order website.', $skipped) ); } } catch (\Throwable $exception) { $this->messageManager->addErrorMessage($exception->getMessage()); } return $this->redirectToGrid(); } private function redirectToGrid() { return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT) ->setPath('*/*/index'); } }