Initial commit

This commit is contained in:
James Oakley 2025-05-20 14:57:40 +00:00
commit 34d112f1e4
7 changed files with 980 additions and 0 deletions

View file

@ -0,0 +1,161 @@
<?php
/**
* Copyright James Oakley 2025. https://git.oakleys.org/
*
* This file is part of Jro_Guest2Customer.
*
* Jro_Guest2Customer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
* Jro_Guest2Customer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
* You should have received a copy of the GNU General Public License along with
* Jro_Guest2Customer. If not, see https://www.gnu.org/licenses/.
*/
namespace Jro\Guest2Customer\Observer;
use \Magento\Customer\Model\AddressFactory;
use \Magento\Customer\Model\CustomerFactory;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer as Mag_Obs;
use \Magento\Framework\Message\ManagerInterface;
use \Magento\Sales\Api\OrderCustomerManagementInterface;
use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Sales\Model\OrderFactory;
use \Magento\Store\Model\StoreManagerInterface;
class CreateCustomerAccount implements ObserverInterface
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected StoreManagerInterface $storeManager;
/**
* @var \Magento\Sales\Api\OrderCustomerManagementInterface
*/
protected OrderCustomerManagementInterface $orderCustomerManager;
/**
* @var \Magento\Sales\Model\OrderFactory
*/
protected OrderFactory $orderFactory;
/**
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
protected OrderRepositoryInterface $orderRepository;
/**
* @var \Magento\Customer\Model\CustomerFactory
*/
protected CustomerFactory $customerFactory;
/**
* @var \Magento\Customer\Model\AddressFactory
*/
protected AddressFactory $addressFactory;
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected ManagerInterface $messageManager;
/**
* Constructor, using DI
* @param \Magento\Sales\Model\OrderFactory $orderFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
* @param \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerManager
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
* @param \Magento\Customer\Model\AddressFactory $addressFactory
* @param \Magento\Framework\Message\ManagerInterface $messageManager
*/
public function __construct(
OrderFactory $orderFactory,
StoreManagerInterface $storeManager,
CustomerFactory $customerFactory,
OrderCustomerManagementInterface $orderCustomerManager,
OrderRepositoryInterface $orderRepository,
AddressFactory $addressFactory,
ManagerInterface $messageManager
) {
$this->orderFactory = $orderFactory;
$this->storeManager = $storeManager;
$this->customerFactory = $customerFactory;
$this->orderCustomerManager = $orderCustomerManager;
$this->orderRepository = $orderRepository;
$this->addressFactory = $addressFactory;
$this->messageManager = $messageManager;
}
/**
* Execute method for the Observer
*
* @param \Magento\Framework\Event\Observer $observer
*/
public function execute(Mag_Obs $observer)
{
// Get order Id.
$orderIds = $observer->getEvent()->getOrderIds();
$orderId = $orderIds[0];
// Load the order with this ID into the order factory.
$order = $this->orderFactory->create()->load($orderId);
// Retrieve the billing and shipping addresses for this order.
$billingAddress = $order->getBillingAddress();
$shippingAddress = $order->getShippingAddress();
// Create customer factory; load the customer with this order's email.
$customer= $this->customerFactory->create();
$customer->setWebsiteId($this->storeManager->getStore()->getWebsiteId());
$customer->loadByEmail($order->getCustomerEmail());
// If the customer factory does not have a customer ID, they don't exist.
$shippingSet = false;
if ($order->getId() && !$customer->getId()) {
$newCustomer = true;
// Create a new customer for this order.
// An instance of CustomerInterface will be returned.
$customer = $this->orderCustomerManager->create($orderId);
// Set the default billing address for this new customer.
$address = $this->addressFactory->create();
$address->setData($billingAddress->getData());
$address->setCustomerId($customer->getId());
$address->setIsDefaultBilling("1");
// Check if the billing and shipping addresses match.
// If they do, this will also be the shipping address.
if ($billingAddress->getPostcode() == $shippingAddress->getPostcode() &&
($billingAddress->getStreet[0] ?? "") == ($shippingAddress->getStreet[0] ?? "")) {
$address->setIsDefaultShipping("1");
$shippingSet = true;
}
$address->setSaveInAddressBook("1");
$address->save();
$this->messageManager->addSuccessMessage(__("We have created an account so you can" .
" log into this website, track orders, and place future orders more easily. We" .
" have sent you an email with instructions to create a password and log in."));
} else {
// If not, the customer already existed. Link the order to that customer.
$newCustomer = false;
$order->setCustomerId($customer->getId());
$order->setCustomerIsGuest(0);
$this->orderRepository->save($order);
// We don't add the billing address, since we should not change the default.
$this->messageManager->addSuccessMessage(__("You have shopped at this website before." .
" We have linked this order with your existing account for your convenience."));
}
// Lastly, we add the shipping address to the customer.
// Iff this was a new customer, we set this as the default.
// If the shipping address matched billing, we can skip this.
if (!$shippingSet) {
$address = $this->addressFactory->create();
$address->setData($shippingAddress->getData());
$address->setCustomerId($customer->getId());
$address->setIsDefaultShipping($newCustomer ? "1" : "0");
$address->setSaveInAddressBook("1");
$address->save();
}
}
}