I'm working on a custom module for magento2. Trying to override the product view block so I can fetch data from an API and pass it into the view. Having read
- https://community.magento.com/t5/Programming-Questions/Overriding-a-block-in-Magento-2/m-p/6831#U6831
- Magento 2: Override Magento\Catalog\Block\Product\View Block
- DI & Extending a Block on Magento 2
I'm still unsuccessful... Here's what I have in my module (Moxune/Moxune):
Block/Product/View.php
<?php
namespace Moxune\Moxune\Block\Product;
use Magento\Framework\View\Element\Template;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
class View extends \Magento\Catalog\Block\Product\View
{
protected function _toHtml()
{
$this->setModuleName($this->extractModuleName('Magento\Catalog\Block\Product\View'));
return parent::_toHtml();
}
}
etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<preference for="Magento\Catalog\Block\Product\View" type="Moxune\Moxune\Block\Product\View" />
</config>
I then run
rm -rf var/cache var/di var/generation var/page_cache && ./bin/magento setup:di:compile
and navigate to a product detail page. I can tell the overridden block isn't getting called though, because I've changed the _toHtml()
body to:
protected function _toHtml()
{
die('wtf');
}
and also introduced syntax errors and the page continues to load fine. However, if I put a die
statement in the _toHtml()
of
var/generation/Magento/Catalog/Block/Product/View/Interceptor.php
that shows on the product detail page. So it seems the preference I'm configuring is having no effect?
FWIW - It seems the DI process is creating a new Interceptor based on the configured preference:
var/generation/Moxune/Moxune/Block/Product/View/Interceptor.php
What am I missing?
get_class($block);
in your product view template ?./vendor/magento/module-catalog/view/frontend/templates/product/view
directory. Added aget_class($block)
in thedetails.phtml
file and getMagento\Catalog\Block\Product\View\Description
view/frontend/templates/product/view.phtml
view.phtml
file, all that's in theview/frontend/templates/product
directory iscompare gallery.phtml image.phtml image_with_borders.phtml list listing.phtml list.phtml view widget
, inside theview
directory areadditional.phtml addto.phtml attributes.phtml details.phtml gallery.phtml opengraph options.phtml review.phtml addtocart.phtml attribute.phtml description.phtml form.phtml mailto.phtml options price_clone.phtml type