init
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\LegacyOrder\Controller\Adminhtml\Order;
|
||||
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Controller\ResultFactory;
|
||||
|
||||
class Link extends Action implements HttpPostActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Esun_LegacyOrder::orders';
|
||||
|
||||
public function __construct(Action\Context $context, private readonly ResourceConnection $resource)
|
||||
{
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$orderId = (int)$this->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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user