Skip to content

Commit

Permalink
Issue #18 - Convert to Customer to working anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Nov 22, 2024
1 parent 06da6c0 commit cf397a1
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions Controller/Adminhtml/Customer/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\Auth\Session;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderCustomerManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Store\Model\App\Emulation;
use Magento\Store\Model\StoreManagerInterface;
use MagePal\GuestToCustomer\Helper\Data;

/**
Expand All @@ -32,11 +34,6 @@ class Index extends Action
*/
protected $orderRepository;

/**
* @var AccountManagementInterface
*/
protected $accountManagement;

/**
* @var CustomerRepositoryInterface
*/
Expand Down Expand Up @@ -67,11 +64,15 @@ class Index extends Action
*/
private $emulation;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Index constructor.
* @param Context $context
* @param OrderRepositoryInterface $orderRepository
* @param AccountManagementInterface $accountManagement
* @param CustomerRepositoryInterface $customerRepository
* @param OrderCustomerManagementInterface $orderCustomerService
* @param JsonFactory $resultJsonFactory
Expand All @@ -81,24 +82,24 @@ class Index extends Action
public function __construct(
Context $context,
OrderRepositoryInterface $orderRepository,
AccountManagementInterface $accountManagement,
CustomerRepositoryInterface $customerRepository,
OrderCustomerManagementInterface $orderCustomerService,
JsonFactory $resultJsonFactory,
Session $authSession,
Data $helperData,
StoreManagerInterface $storeManager,
Emulation $emulation
) {
parent::__construct($context);

$this->orderRepository = $orderRepository;
$this->orderCustomerService = $orderCustomerService;
$this->resultJsonFactory = $resultJsonFactory;
$this->accountManagement = $accountManagement;
$this->customerRepository = $customerRepository;
$this->authSession = $authSession;
$this->helperData = $helperData;
$this->emulation = $emulation;
$this->storeManager = $storeManager;
}

/**
Expand All @@ -116,7 +117,9 @@ public function execute()

if ($orderId && $order->getEntityId()) {
try {
if ($this->accountManagement->isEmailAvailable($order->getCustomerEmail())) {
//get website id from order

if ($this->isEmailAvailable($order->getCustomerEmail(), $order->getStore()->getWebsiteId())) {
$this->emulation->startEnvironmentEmulation($order->getStoreId(), 'adminhtml');
$customer = $this->orderCustomerService->create($orderId);
$this->emulation->stopEnvironmentEmulation();
Expand Down Expand Up @@ -152,6 +155,27 @@ public function execute()
}
}

/**
* @inheritdoc
*
* @param string $customerEmail
* @param int|null $websiteId
* @return bool
* @throws LocalizedException
*/
public function isEmailAvailable($customerEmail, $websiteId = null)
{
try {
if ($websiteId === null) {
$websiteId = $this->storeManager->getStore()->getWebsiteId();
}
$this->customerRepository->get($customerEmail, $websiteId);
return false;
} catch (NoSuchEntityException $e) {
return true;
}
}

/**
* Is the user allowed to view the blog post grid.
*
Expand Down

0 comments on commit cf397a1

Please sign in to comment.