Magento 2 Override – Failed to Override Magento\Catalog\Block\Product\View Block

blocksmagento-2.1magento2overrides

I need to override product view block but i got an error :

Here's my di.xml :

<?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="Magento\Module_Inject_Session\Block\Product\View"/>
</config>

Here's my new block :

enter image description here

And there is this error and I have no idea what to do about it :

enter image description here

anyone one know where i'm wrong? or what is the correct way to override this file?

Best Answer

Try with below solutions, Replace _ with folder and separate each folder path like below,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="Magento\Catalog\Block\Product\View" type="Magento\Module\Inject\Session\Block\Product\View"/>
</config>

Set your block php file at below location,

app/code/Magento/Module/Inject/Session/Block/Product/View.php

inside php file,

<?php
namespace  Magento\Module\Inject\Session\Block\Product;

class View extends \Magento\Catalog\Block\Product\View{

    //your custom code.....

}