Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mpchadwick opened this issue Jul 20, 2021 · 5 comments

Comments

@mpchadwick
Copy link

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:

  • Place an order with an email address that is not associated with an existing customer account on a store other than the default website's default store
  • In the Magento admin panel click "Convert to customer"

Preconditions

  • A multi-site / multi-store Magento install
@mpchadwick
Copy link
Author

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
Copy link
Contributor

srenon commented Jul 20, 2021

@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
Copy link

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
Copy link

@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
Copy link

Cisca22 commented May 8, 2024

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'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants