Magento 2 – How to Get Store Information in Specific Block

magento2override-block

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 to
Tecksky\Catalog\Block\Product\Specification::__construct() must
implement interface Magento\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):

`
enter image description here

How can I use it in this Block file

Best Answer

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.

Related Topic