2

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

1

3 Answers 3

2

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();
}
}
7
  • Is this the right way to get a website Id in Observer? Code: justpaste.it/732i5
    – zus
    Oct 28, 2020 at 6:42
  • Mage::app()->getStore()->getWebsiteId(); this will return the website id for current store. and this is for magento 1. Oct 28, 2020 at 7:10
  • For magento 2 how to get a website id?
    – zus
    Oct 28, 2020 at 7:17
  • you can see the answer above. this is the correct way to get website id. let me know where you are facing the problem. Oct 28, 2020 at 7:24
  • 1
    Updated Code for you. justpaste.it/74d31 . Hope this helps. Oct 29, 2020 at 5:40
0
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();
0
0
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();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.