I am trying to add a new function into the Magento\Catalog\Block\Product\View by extending the class. I tried to use that function in the template which is getHelloWorld() but I can't seem to get the function to work
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="My\Module\Block\Catalog\Product\View" />
</config>
My/Module/Block/Catalog/Product/View.php
<?php
namespace My\Module\Block\Catalog\Product;
class View extends \Magento\Catalog\Block\Product\View
{
public function getHelloWorld()
{
return 'Hello World';
}
}
design/frontend/theme/folder/Magento_Catalog/templates/product/view/type/default.phtml
<?php $_product = $block->getProduct() ?>
//-----added it here this
<?php echo $block->getHelloWorld() ?>
<?php if ($block->displayProductStockStatus()): ?>
<strong>Availability</strong>:
<?php if ($_product->isAvailable()): ?>
<span class="stock available" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
<span><?= /* @escapeNotVerified */ __('In stock') ?></span>
</span>
<?php else: ?>
<span class="stock unavailable" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
<span><?= /* @escapeNotVerified */ __('Out of stock') ?></span>
</span>
<?php endif; ?>
<?php endif; ?>
public function afterGetProduct(\Magento\Catalog\Block\Product\View $subject, $result) { $this->setData('hello world', 'some value'); return $result; }and with the phtml$block->getHelloWorld()does not work – JustBigBoy12345 May 04 '18 at 00:01$_product->isAvailable()and I was thinking of putting it in the default.phtml where it checks if the product is available or not. – JustBigBoy12345 May 04 '18 at 02:39