I created a custom module that automatically assigns a customer to a company
and customer group
during front-end registration using a new field that allows the customer to enter a company code as a custom customer attribute. I’m using an observer on the customer_register_success
event to do this. We provide the company with the code, and the company provides it to their employees. Employees are then able to use the company code provided to automatically register with their company on their own. That way, they're able to see their company’s Private Catalog immediately and make a purchase.
But I am experiencing a couple issues: after the user registers and their company
and customer group
is assigned, they are logged in, redirected to their account dashboard, but the default public menu/categories are still displayed instead of the expected limited menu with only the included categories from their company’s private catalog.
The private catalog itself is correct in that only the products that are included in the private catalog are shown while navigating the site categories, but the dynamic menu that Magento generates is still the regular public menu (it's not limited to only the categories they should see like it should be). If the user navigates to a category that is not included in their private catalog, the category page still loads but displays zero products. That is not typical behavior; the user should be redirected to /no-route
and shown a 404 instead.
Both problems are resolved if the user logs out and then logs back in. This seems to indicate something to do with the session data, but I’m not sure what. Can anyone shed some light on what might be happening, and point me in the right direction to resolve the issue?
I have tried the following:
- Registering as a customer on the front-end, clear all Magento cache, and refresh the page in hopes that maybe the menu block is caching and just needs cleared during the process. This didn't make a difference.
- Use Xdebug to look around at the various session objects, but to be honest I'm not sure where to look. Does something somewhere in a specific session object need to be updated? How can I debug this to track down what needs to be done?
<?php
namespace <Company>\<ModuleName>\Observer;
...
use Magento\Catalog\Model\Session as CatalogSession;
use Magento\Customer\Model\Session as CustomerSession;
use \Magento\Framework\Session\SessionManagerInterface as CoreSession;
class AssignCustomerToCompanyAndGroupRegister implements ObserverInterface
{
...
protected $_coreSession;
protected $_catalogSession;
protected $_customerSession;
..
* @param CoreSession $coreSession
* @param CatalogSession $catalogSession
* @param CustomerSession $customerSession
*/
public function __construct(
...
CoreSession $coreSession,
CatalogSession $catalogSession,
CustomerSession $customerSession
)
{
...
$this->_coreSession = $coreSession;
$this->_catalogSession = $catalogSession;
$this->_customerSession = $customerSession;
}
/**
* @param Observer $observer
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function execute(
Observer $observer
)
{
$coreSessionData = $this->getCoreSession();
$catalogSessionData = $this->getCatalogSession();
$customerSessionData = $this->getCustomerSession();
...
/**
* @return CoreSession
*/
public function getCoreSession()
{
return $this->_coreSession;
}
/**
* @return CatalogSession
*/
public function getCatalogSession()
{
return $this->_catalogSession;
}
/**
* @return CustomerSession
*/
public function getCustomerSession()
{
return $this->_customerSession;
}
}
The dirty fix is to just log them out, and ask them to relogin for “security purposes” but I’d rather avoid that if possible. Thank you in advance for any suggestions anyone might have!