我的magento 2商店中有多个网站。我正在创建自定义代码,所以我需要使用网站id来访问网站Url,我尝试了很多不同的方法,但我无法获得Url。
$this->_objectManager->getWebsite(1)->getDefaultStore()->getBaseUrl();
我也在尝试另一种方法:
public function __construct(
'Magento'Framework'App'Config'ScopeConfigInterface $scopeConfig,
'Linksture'ApplyCoupon'Model'ApplyCouponFactory $applycouponFactory,
'Magento'SalesRule'Model'ResourceModel'Rule'CollectionFactory $collectionFactory,
'Magento'Store'Model'StoreManagerInterface $storeManager,
'Magento'SalesRule'Model'ResourceModel'Coupon'CollectionFactory $couponcollectionFactory
) {
$this->_scopeConfig = $scopeConfig;
$this->_applycouponFactory = $applycouponFactory;
$this->_collectionFactory = $collectionFactory;
$this->_storeManager = $storeManager;
$this->_couponcollectionFactory = $couponcollectionFactory;
}
public function execute('Magento'Framework'Event'Observer $observer)
{
echo $this->_storeManager->getWebsite(1)->getDefaultStore()->getBaseUrl();
}
在magento 1.x中,使用如下所示。
Mage::app()->getWebsite(1)->getDefaultStore()->getBaseUrl();
假设你有多个网站,一个网站在多个商店下,你会得到所有baseUrl、websiteId和storeId public function __construct(
'Magento'Framework'ObjectManagerInterface $objectManager
) {
$this->_objectManager = $objectManager;
}
public function execute('Magento'Framework'Event'Observer $observer){
$storeManager = $this->_objectManager->get('Magento'Store'Model'StoreManagerInterface');
$websites = $storeManager->getWebsites();
foreach($websites as $website){
foreach($website->getStores() as $store){
$wedsiteId = $website->getId();
$storeObj = $storeManager->getStore($store);
$storeId = $storeObj->getId();
$url = $storeObj->getBaseUrl('Magento'Framework'UrlInterface::URL_TYPE_WEB);
}
}
}