1

I'm new in magento 2. How can I add state/province to product page? I did this instructions, it works on contact page but on product page this error appears: error msg

My block is:

    <?php
namespace Ilho\Region\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\ObjectManager;
class ProductView extends \Magento\Catalog\Block\Product\View
{
    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\Directory\Block\Data $directoryBlock, 
        array $data = []
    ) {
        $this->_productHelper = $productHelper;
        $this->urlEncoder = $urlEncoder;
        $this->_jsonEncoder = $jsonEncoder;
        $this->productTypeConfig = $productTypeConfig;
        $this->string = $string;
        $this->_localeFormat = $localeFormat;
        $this->customerSession = $customerSession;
        $this->productRepository = $productRepository;
        $this->priceCurrency = $priceCurrency;
        $this->directoryBlock = $directoryBlock;
        parent::__construct(
            $context,
            $data
        );
    }

    public function getCountries()
    {
        $country = $this->directoryBlock->getCountryHtmlSelect();
        return $country;
    }
    public function getRegion()
    {
        $region = $this->directoryBlock->getRegionHtmlSelect();
        return $region;
    }
}

1 Answer 1

0

If we are talking about block file in magento it is must to call

\Magento\Framework\View\Element\Template\Context $context,

In the constructor section you have called the context of Catalog block. Do replace in your code

\Magento\Catalog\Block\Product\Context $context,

with

\Magento\Framework\View\Element\Template\Context $context,

Then run the below command from your magento root to see the change

php bin/magento setup:upgrade

Note: Try to avoid the use of Object_manager into your code it is not considered in to the best practice of magento try to create inject its dependencies via block files or any other functions in to the block file only.

Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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