Question

How to get current customer website id in Magento2.3 frontend, to precise in customer_register_success observer.

Was it helpful?

Solution

Assuming you already declared the observer class in events.xml

namespace Bp\StackOverflow\Observer\Frontend\Customer;

class RegisterSuccess implements \Magento\Framework\Event\ObserverInterface {

protected $_storeManager;

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager
){
    $this->_storeManager = $storeManager;
}


public function execute(
    \Magento\Framework\Event\Observer $observer
) {
    //Your Website Id
    $currentWebsiteId = $this->_storeManager->getStore()->getWebsiteId();
}
}

OTHER TIPS

namespace Test\Custom\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Custom extends AbstractHelper {

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context);
    }

    public function getStore() {
        return $this->_storeManager->getStore();
    }

    public function getWebsiteId() {
        return $this->getStore()->getWebsiteId();
    }   
}

.phtml  File Get Website Id

$helper = $this->helper('Test\Custom\Helper\Custom');

echo $current_web_id =  $helper->getWebsiteId();
namespace Test\Test\Observer\Customer;
use Magento\Store\Model\StoreManagerInterface;

class Success implements \Magento\Framework\Event\ObserverInterface 
{
    protected $_storeManager;

    public function __construct(StoreManagerInterface $storeManager)
    {
        $this->_storeManager = $storeManager;
    }

    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $WebsiteId = $this->_storeManager->getStore()->getWebsiteId();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top