ich wollte eine neue Funktion \Magento\Catalog\Block\Product\View
Custom Block Magento 2 isnt't arbeiten
hinzufügen, was ich tat:
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\\": ""
}
}
}
Registrierung.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
Innerhalb dieser Datei:
<?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
In diesem Ordner:
di.xml
Inhalt:
<?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>
Auch im Ordner:
module.xml
Inhalt:
<?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>
Ich binden die Funktion über eine Vorlage zu nennen, in product_detail enthalten wie dies:
/**
* Product view template
*
* @var $block \Company\Module\Block\Product\View
*/
?>
<?php echo $block->trySomething(); ?>
This is just another test
Der Text "Dies ist nur ein weiterer Test" wird auf product_detail korrekt angezeigt, aber die Funktion wird nicht aufgerufen.
Hoffe jemand kann mir helfen.