Magento – Modify Product Title Display

magento2magento2.2

I want to prepend a text before the product name is displayed.

I've created a file product-title.phtml in my local template and referenced this in catalog_product_view.xml like so:

    <referenceBlock name="page.main.title">
        <arguments>
            <argument name="template" xsi:type="string">Local/argento-stripes-custom/Magento_Catalog::product/view/product-title.phtml</argument>
        </arguments>
    </referenceBlock>

Unfortunately it's still rendering the default template title file.

Magento 2.2.7

Any suggestions?

Thanks,
Eddie

I tried implementing Ravi's code as suggested, but that seemed to kill all titles from being displayed. But using that as a base I've tried the following:

Created a module Fws_Title

<?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\Theme\Block\Html\Title" type="Fws\Title\Block\Html\Title" />
</config>
<?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="Fws_Title" setup_version="1.0.3">
    </module>
</config>

My Block code is at Fws\Title\Block\Html & as follows:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Fws\Title\Block\Html;

use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

/**
 * Html page title block
 *
 * @method $this setTitleId($titleId)
 * @method $this setTitleClass($titleClass)
 * @method string getTitleId()
 * @method string getTitleClass()
 * @api
 * @since 100.0.2
 */
class Title extends Template
{
    /**
     * Own page title to display on the page
     *
     * @var string
     */
    protected $pageTitle;
    protected $_registry;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * Provide own page title or pick it from Head Block
     *
     * @return string
     */
    public function getPageTitle()
    {
        if (!empty($this->pageTitle)) {
            return $this->pageTitle;
        }
        return __($this->pageConfig->getTitle()->getShort());
    }

    /**
     * Provide own page content heading
     *
     * @return string
     */
    public function getPageHeading()
    {
        if (!empty($this->pageTitle)) {
            return __($this->pageTitle);
        }
        return __($this->pageConfig->getTitle()->getShortHeading());
    }

    /**
     * Set own page title
     *
     * @param string $pageTitle
     * @return void
     */
    public function setPageTitle($pageTitle)
    {
        $this->pageTitle = $pageTitle;
    }

    public function getProduct()
    {
        if($this->registry->registry('current_product')) {
            return $this->registry->registry('current_product');
        }
        return false;
    }

}

My template is in my custom template directory at \Magento_Theme\Template\Html\Title.phtml and looks like this:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var $block \Fws\Title\Block\Html\Title
 */
$cssClass = $block->getCssClass() ? ' ' . $block->getCssClass() : '';
$title = '';

$_product = $block->getProduct();
if (trim($block->getPageHeading())) {
    if($_product){
        $title = '<span class="base fws-product" data-ui-id="page-title-wrapper" ' .  $block->getAddBaseAttribute() . '>'
            . '<span class="item-title">Item No:</span>' .$block->escapeHtml($block->getPageHeading()) . '</span>';
    }else{
        $title = '<span class="base fws-cms" data-ui-id="page-title-wrapper" ' .  $block->getAddBaseAttribute() . '>'
            . $block->escapeHtml($block->getPageHeading()) . '</span>';
    }

}
?>
<?php if ($title): ?>
<div class="page-title-wrapper<?= /* @escapeNotVerified */ $cssClass ?>">
    <h1 class="page-title"
        <?php if ($block->getId()): ?> id="<?= /* @escapeNotVerified */ $block->getId() ?>" <?php endif; ?>
        <?php if ($block->getAddBaseAttributeAria()): ?>
            aria-labelledby="<?= /* @escapeNotVerified */ $block->getAddBaseAttributeAria() ?>"
        <?php endif; ?>>
         <?= /* @escapeNotVerified */ $title ?>
    </h1>
    <?= $block->getChildHtml() ?>
</div>
<?php endif; ?>

I know the code is not working because I've added an extra class into the title span & this is not showing on the front end.

So that would suggest my code is not being picked up at all?

Any ideas,
Thanks again,
Eddie

Best Answer

You can achieve this by override below block

/app/code/Custom/module/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\Theme\Block\Html\Title" type="Custom\Module\Block\Html\Title" />
</config>

/app/code/Custom/Module/Block/Html/Title.php

    <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Custom\Module\Block\Html;

use Magento\Framework\View\Element\Template;

/**
 * Html page title block
 *
 * @method $this setTitleId($titleId)
 * @method $this setTitleClass($titleClass)
 * @method string getTitleId()
 * @method string getTitleClass()
 * @api
 * @since 100.0.2
 */
class Title extends Template
{
    /**
     * Own page title to display on the page
     *
     * @var string
     */
    protected $pageTitle;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry
    )
    {
        parent::__construct($context);
        $this->registry = $registry;    
    }

    public function getCurrentProduct()
    {      
        if($this->registry->registry('current_product')) {
            return $this->registry->registry('current_product');
        }
        return false;        
    }
}

/app/design/frontend/Custom/Theme/Magento_Theme/templates/html/title.phtml

<?php
$prodcut = $block->getCurrentProduct();
$cssClass = $block->getCssClass() ? ' ' . $block->getCssClass() : '';
$title = '';
if (trim($block->getPageHeading())) {
    if($prodcut){ 
    $title = '<span class="base" data-ui-id="page-title-wrapper" ' .  $block->getAddBaseAttribute() . '>'
        . 'SOMETEXT '.$block->escapeHtml($block->getPageHeading()) . '</span>';
    }else{
    $title = '<span class="base" data-ui-id="page-title-wrapper" ' .  $block->getAddBaseAttribute() . '>'
        . $block->escapeHtml($block->getPageHeading()) . '</span>';
    }
}
?>
<?php if ($title): ?>
<div class="page-title-wrapper<?= /* @escapeNotVerified */ $cssClass ?>">
    <h1 class="page-title"
        <?php if ($block->getId()): ?> id="<?= /* @escapeNotVerified */ $block->getId() ?>" <?php endif; ?>
        <?php if ($block->getAddBaseAttributeAria()): ?>
            aria-labelledby="<?= /* @escapeNotVerified */ $block->getAddBaseAttributeAria() ?>"
        <?php endif; ?>>
        <?= /* @escapeNotVerified */ $title ?>
    </h1>
    <?= $block->getChildHtml() ?>
</div>
<?php endif; ?>

With $product variable we can add condition to check product page.