2

I would like to extend \Magento\Catalog\Block\Product\View but I get 404

<?php namespace Vendor\Custom\Block; use Magento\Catalog\Model\ProductFactory; use Magento\Catalog\Block\Product\AbstractProduct; use Magento\Framework\View\Element\Template; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\Product; class MyProducts extends \Magento\Catalog\Block\Product\View { protected $productFactory; protected $_coreRegistry; /** * @param \Magento\Catalog\Block\Product\Context $context * @param array $data */ public function __construct( \Magento\Catalog\Block\Product\Context $context, ProductFactory $productFactory ) { $this->productFactory = $productFactory; $this->_coreRegistry = $context->getRegistry(); parent::__construct($context); } } ?> 

than I created in my module a di.xml which looks like

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <preference for="Magento\Catalog\Block\Product\View" type="Vendor\Custom\Block\MyProducts"/> </config> 

than I reference in product view as follows

<referenceBlock name="product.info.details"> <block class="Vendor\Custom\Block\Variants" name="variants.tab" template="Vendor_Custom::variants.phtml" group="detailed_info"> <arguments> <argument translate="true" name="title" xsi:type="string">Product variations</argument> </arguments> </block> </referenceBlock> 

1 Answer 1

4

Your constructor does not respect the parameters order of the parent class \Magento\Catalog\Block\Product\View, which looks like this:

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, 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; parent::__construct( $context, $data ); } 

So to fix your class, your constructor must look like this:

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, ProductFactory $productFactory array $data = [] ) { $this->productFactory = $productFactory; $this->_coreRegistry = $context->getRegistry(); parent::__construct( $context, $urlEncoder, $jsonEncoder, $string, $productHelper, $productTypeConfig, $localeFormat, $customerSession, $productRepository, $priceCurrency, $data ); } 
5
  • thanks a lot for your feedback I just corrected the constructor and added inside var_dump('test'); exit; but still getting 404 how do I find out what is causing the problem? Commented Apr 18, 2016 at 12:09
  • @fefe try to delete var/cache,var/generation and var/page_cache/ Also check your logs under the var folder, 404 in Magento 2 often means an error happened. Commented Apr 18, 2016 at 12:11
  • I just checked my logs and I see main.CRITICAL: exception 'Magento\Framework\Exception\LocalizedException' with message product.info.options has a child default Commented Apr 18, 2016 at 12:15
  • seems to be a problem in constructor [2016-04-18 12:43:46] main.CRITICAL: exception 'Exception' with message 'Recoverable Error: Argument 1 passed to Vendor\Custom\Block\Variants::__construct() must be an instance of Magento\Catalog\Block\Product\Context, instance of Magento\Backend\Block\Template\Context given, called in /Users/Sites/bsw/var/generation/Vendor/Custom/Block/Variants/Interceptor.php on line 14 and defined in /Users/Sites/bsw/app/code/Vendor/Custom/Block/Variants.php on line 20' in … Commented Apr 18, 2016 at 12:46
  • 1
    hm this has was an issue wit di compile thanks a lot!! Commented Apr 18, 2016 at 13:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.