Implementing Shared Customer Login Across Multiple Magento2 Websites
In today’s e-commerce landscape, it is common for businesses to operate multiple websites targeting different regions or customer segments. One of the key challenges in this setup is managing customer accounts across these separate domains while maintaining a seamless shopping experience.
Suppose you have a group of websites like uae.abc.com, in.abc.com, and uk.abc.com. Each of these sites might have different currency settings, payment methods, pricing strategies, and inventory management. Your goal is to allow customers to log in using a common account across all these sites within the abc.com group.
Understanding Magento2 Customer Scope
Magento provides two options for customer scope: global and website.
-
Global: If you set the customer scope as global, any customer registered on one site can log in to all other sites regardless of their association with a particular website or group. This is the default behavior.
-
Website: For shared login within a specific group of websites (like
abc.com), you would ideally want to use the website scope.
Overriding the Customer Scope Configuration
To achieve this, you need to override the Magento\Customer\Model\Config\Share class and modify the logic for determining when customers can share accounts across multiple sites in your group.
Step 1: Determine Your Website Group IDs
First, identify the store IDs that belong to each of your website groups. In your example, uae.abc.com, in.abc.com, and uk.abc.com likely have IDs like 1, 2, and 3 respectively.
Similarly, for another group like xyz.com, the corresponding store IDs could be 5, 6, and 7.
Step 2: Override the isWebsiteScope Method
In your overridden isWebsiteScope method, you need to check if the current store ID falls within any of these defined groups before returning the default website scope value.
public function isWebsiteScope()
{
$storeId = $this->getStoreManager()->getStore()->getId();
// Check for abc.com group IDs (1-4)
if(in_array($storeId, [1,2,3,4])) {
return 0; // 0 means customer accounts are not shared between these sites
}
// Check for xyz.com group IDs (5-8)
if(in_array($storeId, [5,6,7,8])) {
return 0;
}
// If the store ID does not match any predefined group, return the default scope configuration
return $this->getConfig()->getValue(
self::XML_PATH_CUSTOMER_ACCOUNT_SHARE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) == self::SHARE_WEBSITE;
}
Step 3: Ensure Proper Configuration
Ensure that the customer_account_share configuration option in your store settings is set to ‘Website’.
Conclusion
Implementing shared customer login across multiple Magento2 websites requires a careful understanding of the customer scope configurations and proper override of the relevant classes.
Remember, for this setup to work correctly:
- The customer must be registered on one site within the group (e.g.,
uae.abc.com). - The account sharing configuration (
customer_account_share) in all sites within the group should be set to ‘Website’. - The overridden
isWebsiteScopemethod must accurately identify which websites belong to which groups.
By following this approach, you can provide a seamless and convenient shopping experience for your customers across your multi-site Magento2 e-commerce setup while maintaining the necessary separation between different website groups.