我想在\Magento\Catalog\Block\Product\View
1。 app/code/Company/Module
composer.json
{
"name": "Company/Module",
"description": "N/A",
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0"
},
"type": "magento2-module",
"version": "100.0.2",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\Cms\\": ""
}
}
}
为registration.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Company_Module',
__DIR__
);
2。 app/code/Company/Module/Product/View
在此档案中:
<?php
namespace Company\Module\Block\Product;
class View extends \Magento\Catalog\Block\Product\View {
private function trySomething()
{
exit('test');return '123';
}
}
?>
3。 app/code/Company/Module/etc
在此文件夹中:
di.xml
内容:
<?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="Company\Module\Block\Product\View" />
</config>
同样在文件夹中:
module.xml
内容:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Company_Module" setup_version="1.0.0">
</module>
</config>
我想在模板上调用该函数,包含在product_detail中,如下所示:
/**
* Product view template
*
* @var $block \Company\Module\Block\Product\View
*/
?>
<?php echo $block->trySomething(); ?>
This is just another test
文本&#34;这只是另一个测试&#34;在product_detail上正确显示,但函数不会被调用。
希望有人可以帮助我。
答案 0 :(得分:0)
我自己解决了,问题是块和布局不在同一个模块中。我在view/frontend/layout
下创建了view/frontend/templates
和app/code/local/Company/Module
,这解决了我的问题。