Magento 2 : How to get store information in specific Block
-
06-01-2021 - |
Pergunta
I have Override \Magento\Catalog\Block\Product\View
Block in which i can not get store information.
I have use \Magento\Store\Model\StoreManagerInterface $storeManager
but it throws following error.
Fatal error: Uncaught
TypeError
: Argument 12 passed toTecksky\Catalog\Block\Product\Specification::__construct()
must implement interfaceMagento\Store\Model\StoreManagerInterface,
array given, called in/home/sysadmin/share/magento2/project/generated/code/Tecksky/Catalog/Block/Product/Specification/Interceptor.php
on line 14 and defined in/home/sysadmin/share/magento2/project/app/code/Tecksky/Catalog/Block/Product/Specification.php:12
Stack trace: `#0 /home/sysadmin/share/magento2/project/generated/code/Tecksky/Catalog/Block/Product/Specification/Interceptor.php(14):
Solução
You can directly call store manager in your block file, Just keep below class in your construct,
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Url\EncoderInterface $urlEncoder,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\Stdlib\StringUtils $string,
\Magento\Catalog\Helper\Product $productHelper,
\Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
\Magento\Framework\Locale\FormatInterface $localeFormat,
\Magento\Customer\Model\Session $customerSession,
ProductRepositoryInterface $productRepository,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory,
array $data = []
) {
parent::__construct(
$context,
$urlEncoder,
$jsonEncoder,
$string,
$productHelper,
$productTypeConfig,
$localeFormat,
$customerSession,
$productRepository,
$priceCurrency,
$data
);
$this->groupCollectionFactory = $groupCollectionFactory;
}
If you needed storemanager you can directly call in your file as below way,
$this->_storeManager->getStore()->getId()
Remove generated folder from root and check.
Outras dicas
First, you need to do a compilation
.Note ,before doing compilation,you should delete all files from generate folder
exclude .htaccess
bcoz rest of all files are re-generate at this folder during compilation.
You don't need inject the class Magento\Store\Model\StoreManagerInterface
.
Then In-herniate class Magento\Catalog\Block\Product
already has this class object that can access via $this->_storeManager
as describe by Rakesh Jesadiya and Emipro Technologies Pvt. Ltd
Please follow below code you will get all the store information
app/code/Chapagain/HelloWorld/Block/HelloWorld.php
<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Backend\Block\Template\Context $context,
array $data = []
)
{
parent::__construct($context, $data);
}
/**
* Get store identifier
*
* @return int
*/
public function getStoreId()
{
return $this->_storeManager->getStore()->getId();
}
/**
* Get website identifier
*
* @return string|int|null
*/
public function getWebsiteId()
{
return $this->_storeManager->getStore()->getWebsiteId();
}
/**
* Get Store code
*
* @return string
*/
public function getStoreCode()
{
return $this->_storeManager->getStore()->getCode();
}
/**
* Get Store name
*
* @return string
*/
public function getStoreName()
{
return $this->_storeManager->getStore()->getName();
}
/**
* Get current url for store
*
* @param bool|string $fromStore Include/Exclude from_store parameter from URL
* @return string
*/
public function getStoreUrl($fromStore = true)
{
return $this->_storeManager->getStore()->getCurrentUrl($fromStore);
}
/**
* Check if store is active
*
* @return boolean
*/
public function isStoreActive()
{
return $this->_storeManager->getStore()->isActive();
}
}
?>
See more functions in vendor/magento/module-store/Model/Store.php.
Now, we print the store information in our template (.phtml) file.
echo $block->getStoreId() . '<br />';
echo $block->getStoreCode() . '<br />';
echo $block->getWebsiteId() . '<br />';
echo $block->getStoreName() . '<br />';
echo $block->getStoreUrl() . '<br />';
echo $block->isStoreActive() . '<br />';
Using Object Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
echo $storeManager->getStore()->getStoreId() . '<br />';
echo $storeManager->getStore()->getCode() . '<br />';
echo $storeManager->getStore()->getWebsiteId() . '<br />';
echo $storeManager->getStore()->getName() . '<br />';
echo $storeManager->getStore()->getStoreUrl() . '<br />';
I hope it's clear to you and will help you.
Source: http://blog.chapagain.com.np/magento-2-get-store-information-store-id-code-name-url-website-id/