0

I am new at Magento, I would like to render the product name to catalog_product_view.xml (product page) from a custom phtml but it didn't work. May I know where did I go wrong? Thanks!

app/code/Vendor/Custom_Module/BlockProductName.php

<?php


namespace Vendor\Custom_Module\Block;

use Magento\Catalog\Model\Product;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;

class Popup extends Template
{

/**
 * @var Registry
 */
protected $registry;

/**
 * @var Product
 */
private $product;

public function __construct(Template\Context $context,
                            Registry $registry,
                            array $data)
{
    $this->registry = $registry;

    parent::__construct($context, $data);
}


/**
 * @return Product
 */
private function getProduct()
{
    if (is_null($this->product)) {
        $this->product = $this->registry->registry('product');

        if (!$this->product->getId()) {
            throw new LocalizedException(__('Failed to initialize product'));
        }
    }

    return $this->product;
}

public function getProductName()
{
    return $this->getProduct()->getName();
}

}

product.phtml

<?php

/**
 * @var Vendor\Custom_Module\BlockProductName $block
 */

 $productName = $block->getProductName();
 ?>

 <h1> The product Name: <?php  echo $productName; ?></h1>

app/design/frontend/Vendor/Custom_Module/Magento_Catalog/layout/catalog_product_view.xml

<referenceContainer name="content">
        <block
            class="Magento\Framework\View\Element\Template"
            name="pop_up"
            template="Custom_Module::templates/product.phtml"/>
    </referenceContainer>
4
  • Which page do you want to display the product name from the custom phtml? the product page or the category page? You mentioned you want to do that in catalog_category_view.xml (category page) but you declare the catalog_product_view.xml (product page).
    – Tu Van
    Commented Nov 10, 2022 at 15:16
  • catalog_product_view.xml
    – hill2011
    Commented Nov 10, 2022 at 16:24
  • @hill2011 can you confirm in which layout you want to show the product name? catalog_product_view.xml or catalog_category_view.xml, Also you can update your question.
    – Msquare
    Commented Nov 10, 2022 at 16:38
  • @hill2011 please take a look at my answer, your issue should be fixed.
    – Tu Van
    Commented Nov 11, 2022 at 7:10

2 Answers 2

0

There is something wrong with your code and folder name, but don't worry as you are new at Magento development.

  1. The module name should be named following the camel case: https://en.wikipedia.org/wiki/Camel_case
    Eg: CustomModule
  2. Take a look at my code to learn more. Assume you named the vendor name Vendor, and the module name CustomModule (You can change the vendor name and module name). Take the following steps:

Step 1: Create the registration.php file:
File path: app/code/Vendor/Module/registration.php

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_CustomModule', __DIR__);

Step 2: Create the module.xml file:
File path: 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_CustomModule">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Step 3: Create catalog_product_view.xml layout file:
File path: app/code/Vendor/CustomModule/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page layout="1column" 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 class="Vendor\CustomModule\Block\Product" name="pop_up" template="Vendor_CustomModule::product.phtml"/>
        </referenceContainer>
    </body>
</page>

Step 4: Create Product.php block file:
File path: app/code/Vendor/CustomModule/Block/Product.php

<?php

namespace Vendor\CustomModule\Block;

use Magento\Catalog\Model\Product as ProductModel;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class Product extends Template
{
    /**
     * @var ProductModel
     */
    protected $product = null;

    /**
     * Core registry
     *
     * @var Registry
     */
    protected $coreRegistry = null;

    /**
     * @param Context $context
     * @param Registry $registry
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        $this->coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * @return ProductModel
     */
    public function getProduct()
    {
        if (!$this->product) {
            $this->product = $this->coreRegistry->registry('product');
        }

        return $this->product;
    }
}


Step 5: Create product.phtml template file:
File path: app/code/Vendor/CustomModule/view/frontend/templates/product.phtml

<?php
/** @var \Vendor\CustomModule\Block\Product $block */
?>
<h1><?= $block->escapeHtml(__('The product Name')) ?>: <?= $block->getProduct()->getName() ?></h1>

Step 6: Finally, run the setup:upgrade command to make your new module active, and then compile code, deploy static content:

bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy -f

Done. enter image description here

1
  • If this answer works as your expected, please mark it as accepted. That indicates your issue is solved and makes it easier for others with the same issue to find a verified answer. And click the upvote button for the answer when it is useful.
    – Tu Van
    Commented Nov 10, 2022 at 17:41
0

In the layout file app/design/frontend/Vendor/Custom_Module/Magento_Catalog/layout/catalog_product_view.xml

Replace the content

<referenceContainer name="content">
        <block
            class="Magento\Framework\View\Element\Template"
            name="pop_up"
            template="Custom_Module::templates/product.phtml"/>
    </referenceContainer>

with

<referenceContainer name="content">
        <block
            class="Vendor\Custom_Module\Block\ProductName"
            name="pop_up"
            template="Custom_Module::templates/product.phtml"/>
    </referenceContainer>

Block class need to be changed as per your module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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