magepal / magento2-guest-to-customer

Guest to Customer for Magento2 - Quickly and easily convert existing guest checkout customers to registered customers.
http://www.magepal.com
68 stars 29 forks source link

Customers always created in default website / default store on multisite instance #15

Open mpchadwick opened 3 years ago

mpchadwick commented 3 years ago

Hey @srenon :). Remember me? Hope you are doing well.

We're currently QA-ing this module on a client website (multi-site) and our QA identified the following issue.

Magento version #:

2.4.2-p1

Edition (EE, CE, OS, etc):

EE

Expected behavior:

Customer account is created in website / store that order was placed in

Actual behavior:

Account is always created in default store of default website

Steps to reproduce:

Preconditions

mpchadwick commented 3 years ago

Based on a super quick review it looks like you're using this function:

https://github.com/magento/magento2/blob/2.4.2/app/code/Magento/Sales/Model/Order/OrderCustomerExtractor.php#L94

It seems that here is where it attempts to to retrieve details from the order and map them to the customer.

https://github.com/magento/magento2/blob/2.4.2/app/code/Magento/Sales/Model/Order/OrderCustomerExtractor.php#L94

It seems like maybe an after plugin can be used the map the website / store info from the order to the customer.

srenon commented 3 years ago

@mpchadwick ...

Please try this patch and let me know if it fixes the issues. I currently don't have a multi-store instance setup.

Index: magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php b/magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php
--- a/magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php   (date 1626802192000)
+++ b/magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php   (date 1626802192000)
@@ -15,10 +15,10 @@
 use Magento\Customer\Api\CustomerRepositoryInterface;
 use Magento\Framework\Controller\Result\Json;
 use Magento\Framework\Controller\Result\JsonFactory;
-use Magento\Framework\Exception\LocalizedException;
 use Magento\Sales\Api\Data\OrderInterface;
 use Magento\Sales\Api\OrderCustomerManagementInterface;
 use Magento\Sales\Api\OrderRepositoryInterface;
+use Magento\Store\Model\App\Emulation;
 use MagePal\GuestToCustomer\Helper\Data;

 /**
@@ -62,6 +62,11 @@
      */
     private $authSession;

+    /**
+     * @var Emulation
+     */
+    private $emulation;
+
     /**
      * Index constructor.
      * @param Context $context
@@ -81,7 +86,8 @@
         OrderCustomerManagementInterface $orderCustomerService,
         JsonFactory $resultJsonFactory,
         Session $authSession,
-        Data $helperData
+        Data $helperData,
+        Emulation $emulation
     ) {
         parent::__construct($context);

@@ -92,6 +98,7 @@
         $this->customerRepository = $customerRepository;
         $this->authSession = $authSession;
         $this->helperData = $helperData;
+        $this->emulation = $emulation;
     }

     /**
@@ -110,7 +117,9 @@
         if ($orderId && $order->getEntityId()) {
             try {
                 if ($this->accountManagement->isEmailAvailable($order->getCustomerEmail())) {
+                    $this->emulation->startEnvironmentEmulation($order->getStoreId(), 'adminhtml');
                     $customer = $this->orderCustomerService->create($orderId);
+                    $this->emulation->stopEnvironmentEmulation();
                 } elseif ($this->helperData->isMergeIfCustomerAlreadyExists()) {
                     $customer = $this->customerRepository->get($order->getCustomerEmail());
                 } else {
eBusinessAtlantique commented 1 year ago

I can see the same bug, when converting to a guest to a customer, the store is not correct. Soes the patch above works ? Thanks

eBusinessAtlantique commented 1 year ago

@mpchadwick ...

Please try this patch and let me know if it fixes the issues. I currently don't have a multi-store instance setup.

Index: magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php b/magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php
--- a/magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php (date 1626802192000)
+++ b/magento2ce/app/code/MagePal/GuestToCustomer/Controller/Adminhtml/Customer/Index.php (date 1626802192000)
@@ -15,10 +15,10 @@
 use Magento\Customer\Api\CustomerRepositoryInterface;
 use Magento\Framework\Controller\Result\Json;
 use Magento\Framework\Controller\Result\JsonFactory;
-use Magento\Framework\Exception\LocalizedException;
 use Magento\Sales\Api\Data\OrderInterface;
 use Magento\Sales\Api\OrderCustomerManagementInterface;
 use Magento\Sales\Api\OrderRepositoryInterface;
+use Magento\Store\Model\App\Emulation;
 use MagePal\GuestToCustomer\Helper\Data;

 /**
@@ -62,6 +62,11 @@
      */
     private $authSession;

+    /**
+     * @var Emulation
+     */
+    private $emulation;
+
     /**
      * Index constructor.
      * @param Context $context
@@ -81,7 +86,8 @@
         OrderCustomerManagementInterface $orderCustomerService,
         JsonFactory $resultJsonFactory,
         Session $authSession,
-        Data $helperData
+        Data $helperData,
+        Emulation $emulation
     ) {
         parent::__construct($context);

@@ -92,6 +98,7 @@
         $this->customerRepository = $customerRepository;
         $this->authSession = $authSession;
         $this->helperData = $helperData;
+        $this->emulation = $emulation;
     }

     /**
@@ -110,7 +117,9 @@
         if ($orderId && $order->getEntityId()) {
             try {
                 if ($this->accountManagement->isEmailAvailable($order->getCustomerEmail())) {
+                    $this->emulation->startEnvironmentEmulation($order->getStoreId(), 'adminhtml');
                     $customer = $this->orderCustomerService->create($orderId);
+                    $this->emulation->stopEnvironmentEmulation();
                 } elseif ($this->helperData->isMergeIfCustomerAlreadyExists()) {
                     $customer = $this->customerRepository->get($order->getCustomerEmail());
                 } else {

After using this patch, I have the following error while converting a customer : Attribute is_approved does not contain option with Id new I am using M2.4.6-p1 Any advice on how to make it work correctly ? Thanks

Cisca22 commented 6 months ago

When I try to add a customer in admin I also get the error: Attribute is_approved does not contain option with Id new. I am just migrated to M2.4.6-p4 When a customer want to make an account nothing happens when they click on 'create account'.