Magento2 - How to get current website id in Frontend?
-
12-04-2021 - |
質問
How to get current customer website id
in Magento2.3 frontend, to precise in customer_register_success
observer.
解決
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();
}
}
他のヒント
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();
}
}
所属していません magento.stackexchange