magento 2.2.3 how to add a new cms block or new phtml to product page
Domanda
I want to add a new cms block or PHTML to the product page.
I have the following files composer.json
, etc/module.xml
, and registration.php
I have view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.main">
<block class="Magento\Framework\View\Element\Template" name="staticblock" template="module_name::staticblock.phtml">
</block>
</referenceBlock>
</body>
</page>
I have the .phtml
template file ->
<h1>TEST BLOCK DID IT WORK</h1>
I want this to be appearing after the product info detailed (tabs) like at the bottom
Soluzione
I have created module, Check the below files
app/code/Vendor/Module/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
app/code/Vendor/Module/etc/module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
</module>
</config>
app/code/Vendor/Module/Block/Module.php
<?php
namespace Vendor\Module\Block;
class Module extends \Magento\Framework\View\Element\Template
{
/**
* Constructor
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
parent::__construct($context, $data);
}
/**
* @return string
*/
public function getText()
{
//Your block code
return __('Hello Developer! This how to get the storename: %1 and this is the way to build a url: %2', $this->_storeManager->getStore()->getName(), $this->getUrl('contacts'));
}
}
app/code/Vendor/Module/view/frontend/templates/module.phtml
<?php
/**
* @var $block \Vendor\Module\Block\Module
*/
?>
<div>
<?= $block->getText(); ?>
<h1>TEST BLOCK DID IT WORK</h1>
</div>
app/code/Vendor/Module/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block as="module" class="Vendor\Module\Block\Module" name="module" template="Vendor_Module::module.phtml" after="product.info.details"/>
</referenceContainer>
</body>
</page>
This module is working for me, Please check below screenshot
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange