getRequest()->getParam('id'); $query = trim((string)$this->getRequest()->getParam('customer')); try { if ($orderId <= 0 || $query === '') { throw new \RuntimeException('Enter a customer ID or email.'); } $db=$this->resource->getConnection();$orders=$this->resource->getTableName('esun_legacy_order');$customers=$this->resource->getTableName('customer_entity'); $order=$db->fetchRow($db->select()->from($orders)->where('legacy_order_id=?',$orderId)); if(!$order)throw new \RuntimeException('Legacy order was not found.'); $select=$db->select()->from($customers,['entity_id','website_id','email'])->where('website_id=?',(int)$order['website_id']); if(ctype_digit($query))$select->where('entity_id=?',(int)$query);else$select->where('LOWER(TRIM(email))=?',strtolower($query)); $matches=$db->fetchAll($select); if(count($matches)===0)throw new \RuntimeException('No customer matched in the legacy order website.'); if(count($matches)>1)throw new \RuntimeException('More than one customer matched; use the customer ID.'); $customer=$matches[0];$db->update($orders,['customer_id'=>(int)$customer['entity_id'],'link_status'=>'linked'],['legacy_order_id=?'=>$orderId]); $this->messageManager->addSuccessMessage(__('Legacy order linked to customer #%1 (%2).',$customer['entity_id'],$customer['email'])); }catch(\Throwable$e){$this->messageManager->addErrorMessage($e->getMessage());} return$this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/view',['id'=>$orderId]); } }