0

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

1

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

enter image description here

| improve this answer | |
  • Hello @Kishor I am looking at this now – jibin george Jan 20 at 10:31
  • Nothing appears the phtml doesn't load up unfortunately – jibin george Jan 20 at 10:33
  • 1
    What error you are getting? – Kishor Thummar Jan 20 at 10:35
  • No erros nothing in my debug.log or even system.logs so i am stuck – jibin george Jan 20 at 10:36
  • 1
    I have updated my answer, Please check, Hope this will help you – Kishor Thummar Jan 20 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.